我正在尝试加入 3 个 mysql 表;朋友、用户和评论。
users:
id | firstName | lastName
--------------------------
12 | edwin | leon
9 | oscar | smith
1 | kasandra | rios
friends:
userId | friendID
----------------------
12 | 9
9 | 12
12 | 1
1 | 12
comments:
commenter | comment | commentDate
----------------------------
12 | hey | Oct-2
1 | Hmmmmmm | Nov-1
9 | ok | Nov-2
9 | testing | Nov-2
1 | hello | Dec-20
1 | help | Dec-20
所以我想做的是选择所有用户朋友的评论。所以我想输出的是你朋友的评论:例如:
for edwin leon (id 12) it would output this
friendID | comment | commentDate | firstName | lastName
-----------------------------------------------------------
1 | Help | Dec-20 | kasandra | rios
1 | Hello | Dec-20 | kasandra | rios
9 | testing | Nov-2 | oscar | smith
9 | ok | Nov-2 | oscar | smith
1 | Hmmmm | Nov-1 | kasandra | rios
它会得到所有朋友的评论,但不会得到他的评论。这是我的代码:
SELECT friends.friendID, users.firstName, users.lastName, comments.comment, comments.commentDate
FROM users
JOIN friends ON friends.userID = users.id
JOIN comments ON comments.commenter = friends.friendID
WHERE users.id = '12' AND comments.commenter != '12'
它确实有效,但我没有得到评论者的名字,而是得到了所有评论者的 edwin leon