问个sql语句,如何写效率高
- 天蝎之毒一张表,其他地方从它这里取数据,一对多的关系,我要显示还剩多少没取。比如这样,效率高吗:sql1:
select a.id,a.quantity,b.bqty from test1 a
left join
(
select linkid ,sum(quantity) as bqty from test2 group by LinkID ) b on b.LinkID= a.ID
sql2:
select test1id,quantity ,sum(test2quan) from (
select a.id test1id ,a.quantity,b.quantity test2quan from test1 a inner join test2 b on b.LinkID=a.ID)t
group by test1id,quantity