SQL Server查询:标记查询

时间:2012-02-25 06:26:11

标签: sql sql-server join tagging

我遇到了SQL查询(对于SQL Server)。我是SQL新手,我没有取得多大进展。我已经创建了一个测试标记的测试项目。

我有3张表如下:

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'的所有怪物。

1 个答案:

答案 0 :(得分:2)

假设Monsters.Name中的Tags.ValueName-IDTagLookup组合都是唯一的:

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