简体   繁体   English

在mysql中内部联接多个表

[英]Inner Joining Multiple tables in mysql

I Have three tables in my database users , posts and comments. 我的数据库用户,帖子和评论中都有三个表。

Their Structure is as follow : 它们的结构如下:

**users** : 
user_id , user_name
**posts**: 
post_id , post_content, user_id
**comments** :
comment_id , comment_content , post_id, user_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_id can anyone plz tell me that how this can be done ? 现在我想以以下方式使用join从这三个表中获取数据:comment_id,comment_content,user_id,user_name,post_id任何人都可以告诉我该怎么做? I Shall be very thankful. 我将非常感谢。

It's a simple JOIN . 这是一个简单的JOIN

Try this: 尝试这个:

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: 如果您还需要posts表中的列,请加入它:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM