Click here to Skip to main content
15,891,863 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
for example two columns of table1 , have relation to the id column of table2

for example:

tbl_city: (id , title)
tbl_person: (id , name , CurrentCityID , LastCityID , ...)

now , I want to make two relations from tbl_person to tbl_city
CurrentCityID and LastCityID must relation to tbl_city

this query is wrong:

SELECT tbl_person.name , tbl_city.title , tbl_city.title
FROM tbl_person
JOIN tbl_city ON tbl_person.CurrentCityID = tbl_city.id
JOIN tbl_city ON tbl_person.LastCityID = tbl_city.id

can you help me ?
Posted

Yes it is possible.

Try this:
SQL
SELECT tbl_person.name , tc1.title , tc2.title
FROM tbl_person
JOIN tbl_city tc1 ON tbl_person.CurrentCityID = tc1.id
JOIN tbl_city tc2 ON tbl_person.LastCityID = tc2.id
 
Share this answer
 
You need to identify which table to return results from

SELECT tbl_person.name , C.title AS CurrentTitle, L.title AS LastCity
FROM tbl_person
JOIN tbl_city C ON tbl_person.CurrentCityID = C.id
JOIN tbl_city L ON tbl_person.LastCityID = L.id
 
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