如果选择表A UNION表B:
,我如何通过基于id的结果来查询组以捕获“yy”表A:
id|field1|field2|field3
1 | xx | yy | xx
表B:
id|field1|field2|field3
1 | yy | xx | xx
我想要的结果:
id|field1|field2|field3
1 | yy | yy | xx
答案 0 :(得分:1)
您可以使用自然连接进行此操作并像这样查询
select * from table1
inner join table2
答案 1 :(得分:1)
你可以从工会做最小/最大:
select id, min(field1), min(field2), min(field3)
from (
select id, field1, field2, field3
from table_A
union
select id, field1, field2, field3
from table_B
) as q
答案 2 :(得分:1)
您似乎想要:
select id,
(case when sum(field1 = 'yy') > 0 then 'yy' else max(field1) end) as field1,
(case when sum(field2 = 'yy') > 0 then 'yy' else max(field2) end) as field2,
(case when sum(field3 = 'yy') > 0 then 'yy' else max(field3) end) as field1,
from ((select id, field1, field2, field3
from table_A
) union
(select id, field1, field2, field3
from table_B
)
) ab
group by id;
答案 3 :(得分:0)
我认为这不是那么复杂,所以你需要使用MIN / MAX。你可以在下面试试 -
SELECT A.id, B.field1, A.field2, A.field3
FROM TABLE_A
INNER JOIN TABLE_B
ON A.id = B.id