我有以下高分榜:
CREATE TABLE IF NOT EXISTS `highscores` (
`lid` int(11) NOT NULL,
`username` varchar(15) NOT NULL,
`userid` int(6) NOT NULL,
`score` int(16) NOT NULL,
`dateadded` timestamp NOT NULL default CURRENT_TIMESTAMP,
PRIMARY KEY (`lid`,`username`),
KEY `score` (`lid`,`score`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
并提出以下问题:
1-列出最高分的用户
SELECT
h.userid,h.username ,count(h.username)
FROM
highscores h inner join
(select lid, min(score) as minscore,COUNT( * ) AS totalScores
FROM highscores GROUP BY lid) t on h.lid = t.lid and h.score = t.minscore
GROUP BY h.username ORDER BY `count(h.username)` DESC
2-列出提交分数最多的用户
SELECT username, COUNT( * )
FROM highscores
GROUP BY username
ORDER BY COUNT( * ) DESC
我想要的是一个查询,它同时获得用户总得分和提交的分数,按照得分最高的用户排序。
等等(不确定如何格式化表格) --Username - 高分 - submittedscores
-John --------- 65 -------------- 755
-Jill ------------ 42 -------------- 1435
答案 0 :(得分:1)
select h.username, h.highScores, s.submittedScores
from
(
SELECT
h.username, count(h.username) as highScores
FROM
highscores h inner join
(select lid, min(score) as minscore,COUNT( * ) AS totalScores
FROM highscores GROUP BY lid) t on h.lid = t.lid and h.score = t.minscore
GROUP BY h.username
) as h
inner join
(
SELECT username, COUNT(*) as submittedScores
FROM highscores
GROUP BY username
) as s
on s.username = h.username
order by h.highScores desc, s.submittedScores
我刚拿走了你的两个结果并将它们与内部联接结合起来。不是你需要的吗?
我还没有倾注你的两个问题所以很可能是一个更简单的版本。