mysql order排序时能否用其它表中的数据来排序?
情况是这样的
A表字段user dt dg
a 15 10
b 17 15
B表字段user title jh
a xxx 1
b xxx 0
b xxxxx 0
a xxxxx 0
b xxxxx 1
想要实现的是,根据A表中的user与B表中的user值得出 B表中的有多少与该user相关的数据,按数据条数从多到少排序,然后输出结果为
user dt jh B表中的条数
b 17 1 3
a 15 1 2
------解决方案--------------------
select A.*, T.jh from A, (select user,count(*) as jh from B) T where A.user=T.user order by T.jh desc
------解决方案--------------------
select a.user,a.dt,sum(b.jh) as jh,count(b.user) as counter from a inner join b on a.user=b.user group by b.user order by counter desc;