我需要能夠使用簡單搜索來搜索我的數據庫的SQL查詢。這裏是我現在的表格:搜索多個表(SQL)
Table artists
--------------
id
name
Table albums
-------------
id
artistID
name
Table songs
------------
id
albumID
name
我該如何去做這件事?以下是我嘗試過的一些SQL查詢,但我的問題是它返回了很多數據。例如,如果我搜索像Snoop Dogg這樣的藝術家,那麼即使他們不包含他的名字,它也會爲每張專輯和歌曲返回一行。
SELECT * FROM artist,album,songs WHERE artist.name LIKE '%snoop%' OR albums.name LIKE '%snoop%' OR songs.name LIKE '%snoop%';
編輯:
這裏是一個示例數據庫;
artists
-----------
1 | Snoop Dogg
2 | Linkin Park
albums
--------
1 | artist=1 | Boom
2 | artist=2 | ThisIsIt
songs
--------
1 | album=1 | First
2 | album=2 | Second Linkin
3 | album=2 | Third
4 | album=1 | Fourth
所以我想爲「窺探」的搜索只返回藝術家「史努比狗狗」。但隨後像「Linkin」這樣的搜索將返回藝術家和歌曲。
表歌沒有'artistID'? – safarov 2012-03-21 23:51:35
@safarov他們沒有,但我可以添加它。 – Flipper 2012-03-21 23:53:06
如果沒有,那麼你如何檢測哪首歌屬於藝術家 – safarov 2012-03-21 23:54:02