2014-03-31 71 views
0

選擇我有這樣的MySQL安裝:如何從表條件的其他表

*words* table 
+----+-----------+ 
| id | word | 
+----+-----------+ 
| 1 | curse1 | 
| 2 | curse2 | 
| 3 | niceWord1 | 

*Category* table 
+----+-----------+ 
| id | name | 
+----+-----------+ 
| 1 | niceWords | 
| 2 | badWords | 

*link* table 
+----+--------+-------+ 
| id | wordID | catID | 
+----+-----------+----+ 
| 1 | 1 | 2 | 
| 2 | 2 | 2 | 
| 3 | 3 | 1 | 

我想使一個MySQL SELECT,我說我想用鏈接表中選擇所有不好的話
所以我會得到這樣的輸出:

+----------+------------+---------------+ 
| words.id | words.word | Category.name | 
+----------+------------+---------------+ 
| 1  | curse1 | badWords | 
| 2  | curse2 | badWords | 

我想我需要使用JOIN某種,即時通訊在SQL不太好。

引擎收錄與同:http://pastebin.com/ML684RRA

+2

是加盟,至少會嘗試 –

+0

就像僅供參考它真的是皺着眉頭,當你張貼在多個地方同樣的問題上。一般你應該嘗試一下,然後發佈要求修復/幫助 –

回答

1
select words.id, words.word, Category.name 
from words 
join link on link.wordid = words.id 
join category on category.id = link.catid 
where category.name = 'badWords' 
1

只是通過相關ID的連接表,然後通過使用WHERE壞字ID = 2 ...我還添加別名到篩選出的好話表名,因爲它是一個很好的做法。

SELECT 
    w.word, 
    c.name, 
    c.id 
FROM words w 
JOIN link l on l.wordID = w.id 
JOIN Category c on c.id = l.catID 
WHERE c.id = 2