【发布时间】:2019-07-15 14:42:44
【问题描述】:
我正在尝试在 mysql 中加入三个表。我有三个表,分别称为 post、sharepost 和 user。我想加入 sharepost 和 user 来检索分享帖子的用户的名字和姓氏,我想加入 post 和 user 来检索帖子原始用户的名字和姓氏以及其他列。
post 表有以下列,
postID, title, description, userID, dateposted,likes.
sharepost 表有,
postid, shareuserid, dateshared,likes
用户表有,
userID, firstname, lastname, datejoined, email, password, birthdate.
sharepost 中的 postid 引用 post 表中的 postID。 userID 和 shareuserid 都引用同一个用户表。 我想检索原始用户和分享帖子的用户。
post 表的样本数据是, enter image description here
sharepost 表的样本数据是, enter image description here
用户表的样本数据是, enter image description here
以下查询可以检索分享帖子的用户的名字和姓氏,
SELECT P.postID,P.userID, P.title, P.description, S.shareuserID,
U.firstname, S.dateShared, S.likes from sharepost S join post P
on S.postID=P.postID join user U on S.shareuserID=U.userID
我希望从 post 表中检索原始用户,并从 sharepost 表中检索共享帖子的用户,但我只获取共享用户的名称。
【问题讨论】:
-
请提供示例数据和预期结果。