从所有表中具有相同主键的多个表中获取数据

Muk*_*oor 5 mysql sql select join

我有4个表都具有相同的主键,结构如下:

table_1 : u_id | col1 | col2    

table_2 : u_id | col3 | col4     

table_3 : u_id | col5 | col6    

table_4 : u_id | col7 |  col8
Run Code Online (Sandbox Code Playgroud)

我想获取的数据"col1", "col4", "col6" and "col7"的价值的基础上u_id.

u_id每个表中的值相同.

例如.if u_id='8',然后获取所有指定的列值u_id='8'.

我猜,我没有正确使用连接.

谢谢.

Joh*_*Woo 9

这应该是非常直接的.使用INNER JOIN

SELECT  a.col1, b.col4, c.col6, d.col7
FROM    table1 a
        INNER JOIN table2 b
            ON a.u_id = b.uid
        INNER JOIN table3 c
            ON a.u_id = c.uid
        INNER JOIN table4 d
            ON a.u_id = d.uid
WHERE   a.u_ID = 8
Run Code Online (Sandbox Code Playgroud)

要了解有关联接的更多信息,请参阅以下文章.