选择链接到第三个表中的数据的行

时间:2016-07-12 16:53:23

标签: mysql

我有三张桌子A,B,C

在表A中,我有一个coloumn unit_id

unit_id  id 
3456     45678

表B中有两个coloumn id和project_id

id             project_id
3456           4567

在第三张表C中,我又有两个颜色

project_id     user_id
4567           36547

我想要一个函数,我只输入表A中的id,它给我表C中对应于该unit_id的User_id

1 个答案:

答案 0 :(得分:0)

假设每个unit_id始终只有一个对应的user_id

SELECT A.`unit_id`, C.`user_id` 
FROM A INNER JOIN B ON A.`unit_id` = B.`id` 
INNER JOIN C ON B.`project_id` = C.`project_id`

修改

SELECT C.`user_id` 
FROM A INNER JOIN B ON A.`unit_id` = B.`unit_id` 
INNER JOIN C ON B.`project_id` = C.`project_id`
where A.id= [your id]