有两张表,表A里有列:pot_id, gar_id, pot_value。表B里有列:gar_id, gar_name, gar_volume。其中表B里的主码是gar_id,gar_id也是表A的外码。
如果用两条语句表达,即:select COUNT(*) as gar_remain from A where pot_value = 0 group by gar_id; select gar_id, gar_name, gar_remain from B。怎么把两条语句写成一条?就作像: select gar_id, gar_name, COUNT(*) as gar_remain from A, B ...
------解决方案--------------------------------------------------------
select b.gar_id, b.gar_name, b.gar_remain, gar_id from B
left join
(select gar_id,COUNT(*) as gar_remain from A where pot_value = 0 group by gar_id) c
on b.gar_id=c.gar_id