2012-02-25 85 views
0

我堅持使用SQL查詢(對於SQL Server)。我是SQL新手,並沒有取得太多進展。我創建了一個測試項目來測試標記。SQL Server查詢:標記查詢

我有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 

我試圖選擇具有標記值「綠皮」所有怪物。

回答

2

假設Monsters.NameTags.Value,並在TagLookupName-ID組合都是唯一的:

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