求高手写个mysql语句。叩谢

来源:百度知道 编辑:UC知道 时间:2024/05/11 12:49:17
比如有两个表,大概结构如下:
table1:
id uid name other
1 1 name1 other1
2 2 name2 other2
3 3 name3 other3
4 4 name4 other4
5 5 name5 other5

table2:
id uid gid score
1 1 gid1 score1
2 1 gid2 score2
3 1 gid3 score3
4 3 gid4 score4
5 5 gid5 score5

我想从table2中查询出uid出现次数最多的数据,比如上面的3次,然后求这3次的score之和,并根据出现次数,调用table1中相对应的name值,并按所得的score之和来排序top10.请高手指教这样的语句该如何写。
上面写错了。调用table1中相对应的name值的时候,不是按次数,就是按有同一字段“uid”的关系就行。

SELECT name, count(*), sum(score)
FROM table2 LEFT JOIN table1 ON table1.uid=table2.uid
GROUP BY 1 ORDER BY 3 DESC LIMIT 10

select a.name, b.sum_sco score from table1 a,
(select top 10 uid, sum( score) sum_sco from table2 group by uid order by sum( score) )b
where a.uid = b.uid