简体   繁体   English

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

[英]Fetch data from multiple tables with same primary key in all tables

I have 4 tables all with the same primary key with structure like : 我有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

I want to fetch the data of "col1", "col4", "col6" and "col7" on the basis of the value of u_id . 我想根据u_id的值获取"col1", "col4", "col6" and "col7"u_id

Values of u_id are same in every table. u_id值在每个表中都相同。

For eg. 例如。 if u_id='8' , then fetch all the specified column values where u_id='8' . 如果u_id='8' ,则获取所有指定的列值,其中u_id='8'

I am not using joins correctly, i guess. 我猜,我没有正确使用连接。

Thanks. 谢谢。

This should be pretty straight forward. 这应该是非常直接的。 Use INNER JOIN 使用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

To learn more about joins, please see the article below. 要了解有关联接的更多信息,请参阅以下文章。

SELECT 
table_1.col1, 
table_2.col4, 
table_3.col6, 
table_4.col7 
FROM
table_1,
table_2,
table_3,
table_4
WHERE
table_1.u_id = '8' AND
table_1.u_id = table_2.u_id AND
table_1.u_id = table_3.u_id AND
table_1.u_id = table_4.u_id

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

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