Niks Niks - 4 months ago 11
SQL Question

Selecting a row linked to data in third table

I have three tables A ,B ,C

In table A i have a coloumn unit_id

unit_id id
3456 45678


In Table B have two coloumn id and project_id

id project_id
3456 4567


In third table C i have again two coloumns

project_id user_id
4567 36547


i want a function where i input only the id from table A and it gives me the User_id from table C corresponding to that unit_id

Answer

Assuming every unit_id always has a single corresponding 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`

Edit:

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]