Click here to Skip to main content
16,016,760 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello! I have question for you about difficult LEFT JOIN.
Explaining:
There are 3 tables:

SQL
USERS
_______
Id_USER
Name
Surname

COMMENTS
________
Id_COMMENT
Id_USER
Text

ORDERS
________
Id_ORDER
Name
Id_USER

******************

I make query:

SQL
SELECT USERS.Name, USERS.Surname, COMMENTS.Id_COMMENT, COMMENTS.text, ORDERS.name FROM USERS LEFT JOIN COMMENTS ON
COMMENTS.Id_USER = USERS.Id_USER LEFT JOIN ORDERS ON ORDERS.Id_USER = USERS.Id_USER LEFT JOIN USERS ON ORDERS.Id_USER = USERS.Id_USER


So, i dont know how is correct to make JOIN LEFT again with table USERS if I want connect last table ORDERS with USERS, if i want select information from users, who made order at table ORDERS?

Thank you!
Posted

1 solution

SQL
SELECT USERS.Name, USERS.Surname, COMMENTS.Id_COMMENT, COMMENTS.text, ORDERS.name 
FROM USERS 
LEFT JOIN COMMENTS ON COMMENTS.Id_USER = USERS.Id_USER 
LEFT JOIN ORDERS ON ORDERS.Id_USER = USERS.Id_USER


You only have one User ID listed in the Order table, so you only need one link to the user table.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900