Is it posible to make several relations between two tables in SQL query ?
-
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 ?
H.R
-
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 ?
H.R
Yes, but you may want an
AND
instead. -
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 ?
H.R
Maybe you ar looking for aliases in sql queries? I think this query is the right one for you:
SELECT
person.name as personName, currentCity.title as currentCityTitle, lastCity.title as lastCityTitle
FROM tbl_person as person
JOIN tbl_city as currentCityON person.CurrentCityID = currentCity.id
JOIN tbl_city as lastCity ON person.LastCityID = lastCity.idNote the
as
keyword used in the querry.I have no smart signature yet...
-
Maybe you ar looking for aliases in sql queries? I think this query is the right one for you:
SELECT
person.name as personName, currentCity.title as currentCityTitle, lastCity.title as lastCityTitle
FROM tbl_person as person
JOIN tbl_city as currentCityON person.CurrentCityID = currentCity.id
JOIN tbl_city as lastCity ON person.LastCityID = lastCity.idNote the
as
keyword used in the querry.I have no smart signature yet...
Stanciu Vlad wrote:
Note the as keyword used in the querry.
I've always left the
as
out. -
Stanciu Vlad wrote:
Note the as keyword used in the querry.
I've always left the
as
out.That is a common practice, but in the previous example I assumed the questioner has no or little knoledge about aliases, therefore I followed a more explicit approach. :)
I have no smart signature yet...