用于从两个表中选择数据的单行查询 [英] single line query for selecting data from two tables
本文介绍了用于从两个表中选择数据的单行查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我有两张桌子t1和t2
Hello everyone,
I have two tables t1 and t2
table t1--
id name
1 a
2 b
3 c
4 d
5 e
table t2 --
id name
2 c
3 r
5 z
i want output using a single line query as :
id name
1 a
2 c
3 r
4 d
5 z
< br $>
即时id列在表t1和t2中匹配然后它应该从表t2中选择名称,否则从表t1中选择
i使用临时表完成此操作..但我希望它在一行查询
请帮帮我..
谢谢&问候
krunal panchal
that is when the id column matches in table t1 and t2 then it should select name from table t2 else from table t1
i have done this using temporary tables..but i want it in a single line query
please help me out..
thanks & regards
krunal panchal
推荐答案
这样......
this way...
select t1.id,case when t2.name is null then t1.name else t2.name end as name
from t1
left join t2 on t1.id=t2.id
快乐编码!
:))
Happy Coding!
:)
这篇关于用于从两个表中选择数据的单行查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文