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
解决方法:
您想要将用户表联接到friendId,而不是userid:
SELECT friends.friendID, users.firstName, users.lastName, comments.comment, comments.commentDate
FROM users
JOIN friends ON friends.friendID = users.id
JOIN comments ON comments.commenter = friends.friendID
WHERE friends.userID = '12' AND comments.commenter != '12'
看到它在线运行:sqlfiddle
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。