MySQL从涉及的表中选择

时间:2015-05-06 11:33:55

标签: mysql phpmyadmin

我有两张桌子。
一个表看起来像:user(id,username)
另一个表看起来像:rating(user_id,rating)

现在我想选择查找前10名评分用户:

SELECT *
FROM rating order by rating
LIMIT 10;

现在我想“切换”到用户名并显示前10个ID中的用户名。

我怎么能这样做?

3 个答案:

答案 0 :(得分:1)

SELECT u.username,r.rating
FROM user u
INNER JOIN rating r ON u.id = r.user_id
ORDER BY r.rating DESC
LIMIT 10;

答案 1 :(得分:0)

SELECT u.username, r.rating
FROM rating as r, user as u 
WHERE r.user_id = u.id 
ORDER BY r.rating DESC 
LIMIT 10;

答案 2 :(得分:0)

尝试此查询

SELECT user.username,rating.rating     来自用户     LEFT JOIN等级为user.id = rating.user_id     ORDER BY rating.rating DESC     限制10;