怎么提高这个SQL语句的执行效率???
select top 10 id,name,(select max(bid) from t2 where id=t1.id) as bid,(select bname from t2 where bid=(select max(bid) from t2 where id=t1.id)) as bname from t1 order by id desc
列出t1表中的最新10条记录,同时取出t1表中id在t2表中最新的bid和bname列。
我就是想要这样的结果,但这样定执行太慢了。
------解决方案--------------------后面的 select max(bid) from t2 where id=t1.id 可以直接用bid
------解决方案--------------------你的数据库建索引了吗?
------解决方案--------------------你这样肯定慢了,
select top 10 t1.id,t1.name,t3.bid t2.bname from t1,t2, (select max(bid) as bid ,id from t2 group by id ) t3 where t1.id=t3.id and t2.id=t3.id order by t1.id desc