Alex Guerin
Alex Guerin

Reputation: 2386

SQL Server query: join query

I have the following tables below:

Monster:  
Name     Description              EatsPeople 
Vampire  Pale, afraid of light    True 
Ghost    See-through, annoying    False 
Wraith   Green-ish, ugly, dumb    True 

TagLookup: 
Name    ID 
Ghost   1 
Ghost   2 
Wraith  1 

Tags: 
ID    Text                      Value 
1     Green                     green-skin 
2     Screams like a banshee    banshee-call 

To return any monsters with the 'green-skin' tag applied, I do this:

  SELECT m.Name, m.Description, m.EatsPeople 
  FROM dbo.Monster AS m 
  INNER JOIN dbo.TagLookup AS tl 
    ON m.Name = tl.Name 
  INNER JOIN dbo.Tags AS t 
    ON t.ID = tl.ID 
    AND t.Value = 'green-skin'; 

But how would I also return the text field of the Tags table along with the monster info?

Upvotes: 0

Views: 229

Answers (1)

Ahmad Mageed
Ahmad Mageed

Reputation: 96477

Include it along with the other items, referring to it by t.Text:

SELECT m.Name, m.Description, m.EatsPeople, t.Text

Upvotes: 1

Related Questions