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”和“col7”的数据。
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.
谢谢。
2 个解决方案
#1
6
This should be pretty straight forward. Use 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.
要了解有关连接的更多信息,请参阅下面的文章。
- Visual Representation of SQL Joins
- SQL连接的可视化表示
#2
1
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
#1
6
This should be pretty straight forward. Use 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.
要了解有关连接的更多信息,请参阅下面的文章。
- Visual Representation of SQL Joins
- SQL连接的可视化表示
#2
1
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