我有三個表:如何從MySQL中的三個表中獲取數據?
- gallery_items
- gallery_album_items
- gallery_tags
我已經成功地加入了前兩個表,但我怎麼了gallery_tags表添加到下面的查詢?:
SELECT *
FROM gallery_items
RIGHT JOIN gallery_album_items
ON gallery_items.id=gallery_album_items.id
WHERE gallery_items.name LIKE '%{$term}%'
OR gallery_items.description LIKE '%{$term}%'
本質上是我想補充的:
OR gallery_tags.tag LIKE '%{$term}%'
表結構
gallery_items
id name description
12 Sharon This is sharon's description
26 Rhett This is rhett's description
33 Lincoln This is lincoln's description
gallery_album_items
id album
12 14
26 12
33 7
gallery_tags
id item tag
23 12 blonde
43 26 tall
3 33 glasses
本質上,結果我要爲 「金髮女郎」 的搜索: 沙龍是金髮,是專輯#14。
(你不必出示迴音聲明這僅僅是一個例子向您展示的結果。)
編輯 - 最後的答案
SELECT DISTINCT gallery_items.id
FROM gallery_items
RIGHT OUTER JOIN gallery_album_items ON gallery_items.id = gallery_album_items.id
LEFT JOIN gallery_tags ON gallery_items.id = gallery_tags.item
WHERE gallery_items.active != '0'
AND (gallery_items.name LIKE '%blonde%'
OR gallery_items.description LIKE '%blonde%'
OR gallery_tags.tag LIKE '%blonde%'
僅供參考,請注意,RIGHT JOINs的出現時間大約是LEFT JOINs的1/20倍 - 只是說 – Strawberry