SQL Server查询:连接查询

时间:2012-02-26 04:38:16

标签: sql sql-server tsql

我有以下表格:

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 

要返回任何应用了'green-skin'标签的怪物,我会这样做:

  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'; 

但是,我如何同时返回Tags表的文本字段以及怪物信息?

1 个答案:

答案 0 :(得分:1)

将其与其他项目一起包括在内,并通过t.Text引用它:

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