假设有这样一个表
--------------
Name | Friend
--------------
a b
b c
c d
d b
--------------
如何在mysql中实现如下排序结果
-------------------------
Name | Friend | citedBy
-------------------------
b a 2
b d 2
c b 1
d c 1
-------------------------
friend和name字段颠倒并排升序很容易搞定前2项,但是怎么样将统计数字合并到对应记录上,没什么头绪,
应该是用procedure的,刚学mysql不知道怎么写,望大虾指教!
另:procedure里in/out array和procedure内部定义使用array的方法,望指教!可以的话以上面的table
为例给个sample code。
先多谢了!
--------------
Name | Friend
--------------
a b
b c
c d
d b
--------------
如何在mysql中实现如下排序结果
-------------------------
Name | Friend | citedBy
-------------------------
b a 2
b d 2
c b 1
d c 1
-------------------------
friend和name字段颠倒并排升序很容易搞定前2项,但是怎么样将统计数字合并到对应记录上,没什么头绪,
应该是用procedure的,刚学mysql不知道怎么写,望大虾指教!
另:procedure里in/out array和procedure内部定义使用array的方法,望指教!可以的话以上面的table
为例给个sample code。
先多谢了!
a.name,
a.friend,
b.cnt as citeBy
from
tb a,
(select friend,count(*) as cnt from tb group by friend) b
where
a.friend=b.friend
order by
b.cnt desc,friend
select
a.name as friend,
a.friend as name,
b.cnt as citeBy
from
tb a,
(select friend,count(*) as cnt from tb group by friend) b
where
a.friend=b.friend
order by
b.cnt desc,friend
LEFT outer join (SELECT friend,COUNT(*) citedBy from tt GROUP by friend ) b
on b.friend=tt.friend ORDER by tt.friend;
josy的回答,我在真实的10万条数据中执行过了,加上citedBy下限过滤后还是能出正常结果。
mathematician的回答不加过滤的话也应该是正常的,因为数据量大没有具体查。
不过加上过滤后就出现了citedBy字段为null的情况。
总之问题解决,非常感谢。