1

让我直奔问题。这是我的 sql fiddle sql fiddle

现在我的要求是在以下条件下使用表 A 的连接语句从表 B 或表 C 中检索名称列

If result is present in table B retrieve name from table B and ignore table C
else retrieve name field from table c

基本上我需要以下结果

1 AB name1
2 BC name2
3 CD name3
4

3 回答 3

2
select id,A.column1,D.name from A left join 
(select * from B union select * from C) as D on A.column1=D.column1
于 2013-10-23T05:34:23.417 回答
0
select * from B
UNION 
select * from C where column1 not in (select column1 from B)
order by name

SQL Fiddle 链接:http ://sqlfiddle.com/#!2/5f4f4/16

于 2013-10-23T05:34:15.023 回答
0

你不需要做你提到的 if 语句。只需尝试以下操作:

 select A.id, A.column1 from A left join B on A.column1 = B.column1 
 left join C on C.column1 = A.column1;

SQL小提琴

于 2013-10-23T05:35:26.283 回答