[英]How to fetch results from user,post,comments tables in one query
标题可能不完全适合这个问题! 对不起
目标
我要实现的name
中的user
谁做了post
,将description
该职位(的p_description
),是那里的意见post
,如果谁提供意见的名称( comment_description
)
我怎样才能做到这一点?
我有三张桌子:
user
post
和
comments
用户表如下:
u_id
name
等......
邮政表,这里是:
u_id p_id
p_description
u_id
作为user
表的外键,
而Comments
表是:
u_id
p_id
comment_description
。
请注意,表注释中的u_id和p_id分别是表user
和post
外键。
我正在为上述目标编写以下查询:
SELECT p_description,f_name,comment_description as COMMENT FROM user JOIN post ON user.u_id = post.u_id JOIN comments ON user.u_id = comments.u_id
它给了我post description
,发post description
name of that guy who has done post
,以及the comments on that post
name of that guy who has done post
the comments on that post
,我想也有那些对那篇文章做过评论的人的名字 。
如果问题没有得到很好的解释。
我只想查询关于这三个表的上述目标 。
在comments
再次加入user
ID。
SELECT u1.name,
p1.p_description,
u2.name,
c1.comment_description
FROM user u1
INNER JOIN post p1
ON u1.u_id = p1.u_id
INNER JOIN comments c1
ON c1.p_id = p1.p_id
INNER JOIN user u2
ON u2.p_id = c1.p_id;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.