我有3张桌子
我想选择最喜欢的用户的名称,以及得分最高的用户的名称。但我的问题是,当我编写查询时,我在那里复制了name
字段。我该怎么办?
答案 0 :(得分:2)
使用别名可以帮助您
SELECT a.id as id_a,
b.id as id_b,
c.id as id_c
from a
inner join b
on a.id = b.a_id
inner join c
on a.id = c.a_id
答案 1 :(得分:1)
您可能正在使用SELECT *
。您应该指定字段名称:
SELECT u.user_id, u.name, li.numOfLikes FROM users u INNER JOIN likes li ON (u.user_id=li.user_id) ORDER BY li.numOfLikes
SELECT u.user_id, u.name, s.score FROM users INNER JOIN scores s ON (u.user_id=s.user_id) ORDER BY s.score
答案 2 :(得分:1)
我在查询中使用了以下三个表
要获取具有最高分数的用户的详细信息,请使用以下
select usrtbl.username,usrtbl.userid,tbl2.score from usertable usrtbl inner join table2 tbl2 on usrtbl.userid=tbl2.userid order by tbl2.score
desc limit 1
要获取最多喜欢的用户的详细信息,请使用以下
select usrtbl.username,usrtbl.userid,tbl3.numOfLikes from usertable usrtbl inner join table3 tbl3 on usrtbl.userid=tbl3.userid order by tbl3.numOfLikes
desc limit 1
要在一个查询中使用以下查询
select usrtbl.username as 'user_with_max_score',usrtbl.userid as 'userid_of_user_with_max_score',
tbl2.score as 'max_score',usrtbl2.username as 'user_with_max_likes',usrtbl2.userid as 'userid_of_user_with_max_score' ,
tbl3.numOfLikes as 'max_likes' from usertable usrtbl2 inner join table3 tbl3 inner join usertable usrtbl inner join table2 tbl2
on usrtbl.userid=tbl2.userid and usrtbl2.userid=tbl3.userid order by tbl2.score desc,tbl3.numOfLikes desc
limit 1
答案 3 :(得分:0)
您可以选择 tablename .name