本文介绍了在mysql中内部联接多个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的数据库中有三个表用户,帖子和评论.
I Have three tables in my databaseusers , posts and comments.
它们的结构如下:
**users** :
user_id , user_name
**posts**:
post_id , post_content, user_id
**comments** :
comment_id , comment_content , post_id, user_id
现在我想以以下方式使用join从这三个表中获取数据:comment_id,comment_content,user_id,user_name,post_id谁能告诉我该怎么做?我应该非常感谢.
now i want to fetch data from these three tables using join in a following way :comment_id, comment_content, user_id, user_name, post_idcan anyone plz tell me that how this can be done ?I Shall be very thankful.
推荐答案
这是一个简单的JOIN
.
尝试一下:
select c.comment_id,
c.comment_content,
u.user_id,
u.user_name,
c.post_id
from comments c
join users u on u.user_id = c.user_id;
如果您还需要发件表中的列,请加入它:
If you need columns from posts table too, join it:
select c.comment_id,
c.comment_content,
u.user_id,
u.user_name,
p.post_id,
p.post_content
from comments c
join users u on u.user_id = c.user_id
join posts p on c.post_id = p.post_id;
这篇关于在mysql中内部联接多个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!