Postgresql 在postgres中合并两个select查询结果
需要合并postgres中的两个select查询结果,而不使用union或union。所有bcz数据类型都不同。我可以在php中完成这项工作,但仍然尝试获取查询结果本身 我试过用union,它说数据类型不同 查询1:Postgresql 在postgres中合并两个select查询结果,postgresql,Postgresql,需要合并postgres中的两个select查询结果,而不使用union或union。所有bcz数据类型都不同。我可以在php中完成这项工作,但仍然尝试获取查询结果本身 我试过用union,它说数据类型不同 查询1: (select a.id,a.op_id,a.consult_id,b.id,b.finding_value,b.test_name_id,b.methd_id,b.sngl_tst_id,b.grp_tst_id,b.cmplx_tst_rslt_id,b.test_ty
(select a.id,a.op_id,a.consult_id,b.id,b.finding_value,b.test_name_id,b.methd_id,b.sngl_tst_id,b.grp_tst_id,b.cmplx_tst_rslt_id,b.test_type
from tra_rslt_entry_hd a
join tra_rslt_entry_dt b on b.result_id = a.id
where b.test_type = 'S' and a.consult_id = 5849 )
查询2:
(select distinct a.op_id,a.consult_id,d.test_name_id,d.specimen,max(g.male)as male,max(g.female) as female,max(g.common) as common ,max(g.adult) as adult,max(g.child) as child,max(g.gender_specific)as gender_specific, d.test_name
from tra_op_ip_consult_head a
join tra_op_ip_diagno_detail c on c.consult_id=a.consult_id
join mas_test_name d on d.test_name_id = c.test_name_id
join mas_tst_rgnt_nmval g on g.test_name_id = d.test_name_id
where a.consult_id =5849 and d.dept_id = 6 group by a.consult_id,a.op_id,d.test_name_id,d.test_name,d.specimen
order by d.test_name_id)
若要使用UNION运算符,所有查询的列数必须相同,并且列的类型必须兼容,因此必须使用类型转换 假设query1(b.id)中的第四列是数字,query2(d.sample)中的第四列是文本。 联合将失败,因为它们是不兼容的类型。 您必须使用类型转换:
select a.id,a.op_id,a.consult_id,b.id::text, ...
union
select distinct a.op_id,a.consult_id,d.test_name_id,d.specimen,...