日期:2014-05-17  浏览次数:20657 次

怎么样才能合并sql并按b表的字段排序啊
a表sql:select a.id,a.userid,a.username,a.shangjianame
from zu_car_old_shangjia a

b表sql:select count(id) as carnum,userid from zu_car_old
  where passok=1
  group by userid

现在我想把上面sql合成一句,以a表为主,按b表查询出来的carnum从大到小排,两表的userid是相等的,即按发布车的数量,把商家排出来?

------解决方案--------------------
SQL code
select a.id,a.userid,a.username,a.shangjianame,b.carnum
from zu_car_old_shangjia a
join
(select count(id) as carnum,userid from zu_car_old
  where passok=1
  group by userid
) b
on a.userid=b.userid
order by b.carnum desc