select tb1.*, (select name from tb2 where tb2.id=tb1.id1) as name1, (select name from tb2 where tb2.id=tb1.id2) as name2, (select name from tb2 where tb2.id=tb1.id3) as name3
from tb1
有朋友说这样写效率低,但哥们我不知道更高效率的写法,或,更好的解决方法
请大侠们鼎力相助,帮我解决一下,有什么更好的方法或有什么更好的sql语句的写法,,谢谢大家
------解决方案-------------------- select tb1.*,T1.Name as name1,T2.Name as name2,T3.Name as name3 from tb1 inner join tb2 T1 on T1.id=tb1.id1 inner join tb2 T2 on T2.id=tb1.id2 inner join tb2 T3 on T3.id=tb1.id3
------解决方案-------------------- 兄弟,你的那个方法是子查询,子查询效率很低的。我的方法是内连接,集合操作,当然效率高多了
------解决方案-------------------- select tb1.*,name as name1,name as name2,name as name3
from tb1 这样部行吗?
------解决方案--------------------
------解决方案-------------------- select tb1.*, (select name from tb2 where tb2.id=tb1.id1) as name1, (select name from tb2 where tb2.id=tb1.id2) as name2, (select name from tb2 where tb2.id=tb1.id3) as name3
from tb1
这好像比表连接查询的效率要好
slect t.* ,t.nameaa as name1,t.nameaa as name2,t.nameaa as name3 from( select tb1.*, (select name from tb2 where tb2.id=tb1.id1) as nameaa