Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL query that returns value based on lookup of id in another table

Tags:

sql

I have 2 tables:

    dbo.Events

    EventID               EventName            Location
    1                     Birthday Party       2
    2                     Wedding              1

    dbo.EventsLocation

    Location    LocationName
    1           Room 1
    2           Room 2

I would like to make an SQL query that returns the following

    Birthday Party    Room 2
    Wedding           Room 1
like image 729
user1131033 Avatar asked Jan 04 '12 23:01

user1131033


2 Answers

SELECT
  Events.EventName AS EventName,
  EventsLocation.LocationName AS LocationName
FROM
  Events
  INNER JOIN EventsLocation ON Events.Location=EventsLocation.Location
(WHERE ...)
;
like image 122
Eugen Rieck Avatar answered Nov 13 '22 14:11

Eugen Rieck


Join the tables:

select e.EventName, l.LocationName
from Events e
inner join EventsLocation l on l.Location = e.Location
like image 2
Guffa Avatar answered Nov 13 '22 14:11

Guffa