如何从 INNER JOIN 表中选择一个字段和其他字段的 DISTINCT
我使用 PHP 和 MySQL,并且我 有 2 个表:
- 表 Score:有字段“user_id”和“score”
- 表 user:有字段“user_id”和“user_name”
人们玩游戏,然后每次游戏结束时将 user_id 和分数存储在表“score”中。表分数中单个用户有许多行。
我想要什么? 我想选择具有不同 user_name 的前 10 名玩家显示在列表中,那么正确的 sql 是什么?下面的代码现在是我当前的sql,但它没有显示我想要的结果。
SELECT DISTINCT * FROM score as t1
INNER JOIN user AS t2 ON t1.user_id=t2.user_id
WHERE score>0
ORDER BY t1.score DESC
LIMIT 10
这有什么错误呢?
I use PHP and MySQL and I
have 2 tables:
- table score: have fields 'user_id' and 'score'
- table user: have fields 'user_id' and 'user_name'
People play game then store user_id and score in table 'score' every time game was ended. There are many rows for a single user in table score.
What I want?
I want to select top 10 players with distinct user_name to show on the list, so what is the correct sql? Code below is now my current sql, but it's not show result what I want.
SELECT DISTINCT * FROM score as t1
INNER JOIN user AS t2 ON t1.user_id=t2.user_id
WHERE score>0
ORDER BY t1.score DESC
LIMIT 10
What is the mistake for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这有用吗? (未测试)
does this work? (not tested)
以下 sql 将返回按最佳成绩排序的前 10 名最高得分玩家(不是得分前 10 名,而是前 10 名玩家)。
The following sql will return the top 10 highest scoring players sorted by their best score (not a top 10 of scores, but a top 10 of players).