我的情况是这样的:我有 2 个 mysql 表:user和scores。
表用户是这样的:id,name。表分数是这样的:id、point、user_id;
我想取获得高分的 10 个用户的名字,但我不知道我必须使用什么样的查询。有人可以帮助我吗?
最简单的答案是使用ORDER BY
,LIMIT
但它不适用于具有相同点的重复用户。
SELECT a.*, b.*
FROM user a
INNER JOIN scores b
ON a.id = b.user_ID
ORDER BY b.point DESC
LIMIT 10
要处理重复点,您可以有一个包含前 N个分数的子查询。
SELECT a.*, b.*
FROM user a
INNER JOIN scores b
ON a.id = b.user_ID
INNER JOIN
(
SELECT point
FROM scores
GROUP BY point
ORDER BY point DESC
LIMIT 10
) c ON b.point = c.point
ORDER BY b.point DESC
您可能想要使用查询并获取您需要按列降序INNER JOIN
排序并将结果限制为 10 的所有相关数据points
SELECT *
FROM user a
LEFT JOIN scores b
ON a.id = b.user_id
ORDER BY b.point DESC
LIMIT 10
Use this:-
SELECT *
FROM user as u,scores as s Where u.id = s.user_id
ORDER BY s.point DESC
LIMIT 10