Ninad Ninad - 2 months ago 8
SQL Question

How to write query to get names of primary keys

Table structure :

Location
--------------
location_id name
1 Location1
2 Location2
3 Location3

Tour_available
--------------
tour_available_id from_location to_location
1 2 3
2 1 2
3 2 1


In above table from_location and to_location are foreign_keys(Which is primary key in Location table)

I want output as:

Expected output
--------------
from_location to_location
Location2 Location3
Location1 Location2
Location2 Location1

Answer
select FL.name as frm_location, TL.name as t_location 
  from Tour_available as TA INNER JOIN Location as FL ON (FL.location_id = TA.from_location) 
    INNER JOIN Location as TL ON (TL.location_id = TA.to_location);