2015-10-29 51 views
2

我有兩個表和條件在WHERE上多列

Table 1 = restaurants (r_id , type , cuisine_bkup) 
Table 2 = address (id , r_id , location, address) 

我申請這個查詢

SELECT * 
FROM restaurants r 
RIGHT JOIN addresses a ON a.r_id = r.r_id 
WHERE r.type LIKE 'Cafe' AND a.location LIKE 'Gulberg' AND r.cuisine_bkup LIKE 'Pizza' 
GROUP BY r.r_id 

它給我任何結果我應該怎麼做,我想做到這一點的條款,條件

回答

0

使用%like

WHERE r.type LIKE '%Cafe%' 
AND a.location LIKE '%Gulberg%' 
AND r.cuisine_bkup LIKE '%Pizza%' 
1

您可能需要通配符%來像這樣搜索列中的值:

SELECT * 
FROM restaurants r 
RIGHT JOIN addresses a ON a.r_id = r.r_id 
WHERE r.type LIKE '%Cafe%' AND a.location LIKE '%Gulberg%' AND r.cuisine_bkup LIKE '%Pizza%' 
GROUP BY r.r_id 

否則其他選項(我猜是使用代替OR

SELECT * 
FROM restaurants r 
RIGHT JOIN addresses a ON a.r_id = r.r_id 
WHERE r.type LIKE '%Cafe%' OR a.location LIKE '%Gulberg%' OR r.cuisine_bkup LIKE '%Pizza%' 
GROUP BY r.r_id 

另外需要注意的是,你會得到的結果爲您並且當且僅當所有三個條件都爲真時,即,r.type LIKE 'Cafe' AND a.location LIKE 'Gulberg' AND r.cuisine_bkup LIKE 'Pizza'

+0

我希望原來是正確的 - 尋找一個Gulberg的咖啡廳供應比薩餅 - 所有這三個都必須是真實的 - 沒有獲得咖啡館的名單,無論位置/美食+ Gulberg所有餐廳的名單+所有供應披薩的餐館名單。 – PaulF

+0

非常感謝,我在發佈這個問題5分鐘後找到了解決方案......而且你是對的,它工作正常:) –

0
SELECT * 
FROM restaurants r 
RIGHT JOIN addresses a ON a.r_id = r.r_id AND a.location LIKE 'Gulberg' 
WHERE r.type LIKE 'Cafe' AND r.cuisine_bkup LIKE 'Pizza' 
GROUP BY r.r_id 

做出正確加入工作,你必須移動到AND a.location LIKE 'Gulberg'ON section.Otherwise它將作爲INNER JOIN工作。

我想指出的是,「=」會比LIKE快得多(如果你是等同字符)..

希望這有助於