从子表取得数量,回到父表按这数量排序父表记录
如何做?按这关系
父表.id=子表.fk_idselect a.*,b.id,b.title from a,b order by b.id desc
这样子不行,也表达不了标题意思有无其他方法呢?大家明吗。不明我就再说一遍。。
如何做?按这关系
父表.id=子表.fk_idselect a.*,b.id,b.title from a,b order by b.id desc
这样子不行,也表达不了标题意思有无其他方法呢?大家明吗。不明我就再说一遍。。
a 主表 (id int,country varchar(20))
b 子表 (id int,title varchar(20),countryid int)-- 查询
select a.id,a.country,count(*)
from a a join b b
on a.id=b.countryid
group by a.id,a.country
order by count(*) desc-- 结果按主表分组的数量进行排序,应该能满足你的结果了吧.