MySQL 内连接 2 列
我有一个表 users 有 2 个字段: id 、 name ,另一张表 Friends 有 2 个字段: userid1 userid2 。
所以这意味着 userid1 是 userid2 的朋友。我想要,用一个内部或左连接来显示这一点:
userid1 名称(从用户表中获取)是 userid2 名称(从用户表中获取)的朋友
某事
SELECT * FROM Friends INNER JOIN users ON Friends.userid1 = users.id AND Friends .userid2 = users.id 但这不起作用。
有什么好的查询和好的解释吗?
例如,如果我在用户表中有以下详细信息: ID:1 姓名:FinalDestiny ID:2 姓名:George
以及朋友表中的下一个详细信息: ID1 : 1 ID2 : 2
所以这意味着 1 是 2 的朋友。
我需要通过一个查询来获取 1 的名字和 2 的名字并回显他们是朋友
FinalDestiny 是 George 的朋友
I have one table users with 2 fields : id , name and another table friends with 2 fields : userid1 userid2 .
So this means userid1 is friend with userid2 . I want , with one inner or left join to show this :
userid1 name(got from users table) is friend with userid2 name(got from users table)
something
SELECT * FROM friends INNER JOIN users ON friends.userid1 = users.id AND friends.userid2 = users.id but this is not working.
Any good query with good explanation please?
If, for example I have the next details in the users table :
ID : 1 NAME : FinalDestiny
ID : 2 NAME : George
and the next details in the friends table :
ID1 : 1 ID2 : 2
So this means 1 is friend with 2.
I need with one query to get the name of 1 and the name of 2 and to echo that they're friends
FinalDestiny is friend with George
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我知道您指定希望通过一个连接来完成此操作,但在我看来您需要两个连接。我相信此查询将为您提供所需的信息:
SELECT u1.Name, u2.Name
来自用户 u1、用户 u2、朋友 f
WHERE u1.id = f.userid1 AND u2.id = f.userid2
我正在使用 Users.id = Friends.userid1 将 Users 表与 Friends 表连接起来。然后,我将这个新表与 (new table).userid2 = Users.id 上的 Users 表连接起来。这会生成一个包含 4 列的表。这些列是 Friends 表的原始 2 列以及好友姓名的 2 列。
u1
、u2
和f
并引用已连接的三个表。用户表与朋友表连接两次。u1
是在 Friends.userid1 = User.id 上加入的 Users 表的副本。u2
是在 Friends.userid2 = User.id 上加入的 Users 表的副本。在这个有 4 列的表中,我仅选择朋友的姓名。
如果需要更多解释,请告诉我。
I know that you specify that you want this done with one join, but it looks to me like you need two. I believe this query will give you what you need:
SELECT u1.Name, u2.Name
FROM Users u1, Users u2, Friends f
WHERE u1.id = f.userid1 AND u2.id = f.userid2
I am joining the Users table with the Friends table with on Users.id = Friends.userid1. Then, I am joining this new table with the Users table on (new table).userid2 = Users.id. This results in a table with 4 columns. These columns are the original 2 columns of the Friends table in addition to 2 columns for the names of the friends.
u1
,u2
, andf
and referring to the three tables that were joined. The users table was joined with the friends table twice.u1
is the copy of the Users table that was joined on Friends.userid1 = User.id.u2
is the copy of the Users table that was joined on Friends.userid2 = User.id.From this table with 4 columns, I am selecting only the names of the friends.
Let me know if more explanation is needed.
您检查一个用户是否是其自身的好友。在您的选择中使用
OR
来匹配用户的朋友:You check on a user being a friend of itself. Use
OR
in your select to match user's friends:例如,如果我在用户表中有以下详细信息:
ID:1 姓名:FinalDestiny
ID:2 姓名:George
以及朋友表中的下一个详细信息:
ID1 : 1 ID2 : 2
所以这意味着 1 是 2 的朋友。
我需要通过一个查询来获取 1 的名字和 2 的名字并回显他们是朋友
FinalDestiny 是 George 的朋友
If, for example I have the next details in the users table :
ID : 1 NAME : FinalDestiny
ID : 2 NAME : George
and the next details in the friends table :
ID1 : 1 ID2 : 2
So this means 1 is friend with 2.
I need with one query to get the name of 1 and the name of 2 and to echo that they're friends
FinalDestiny is friend with George