2012-02-26 154 views
0

我在下面如下表:SQL Server查詢:連接查詢

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 

要應用了「綠皮」標籤返回任何怪物,我這樣做:

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

但如何我是否也會將標籤表的文本字段與怪物信息一起返回?

+2

您是否嘗試過在'SELECT'聲明加入't.Text'? – Bernard 2012-02-26 04:41:31

回答

1

包括它與其他項目一起,指的是它的t.Text

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

如果沒有另一個查詢,是否可以返回一個布爾值來指示標籤是否應用「無意義」? – 2012-02-26 05:28:11