簡體   English   中英

多列的 WHERE 子句中的 AND 條件

[英]AND condition in WHERE clause on multiple columns

我有兩張桌子

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

它沒有給我任何結果我該怎么做我想用 AND 條件做

您可能需要通配符%來搜索列中的值,如下所示:

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而不是AND

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

還要注意,當且僅當所有三個條件都為真時,您將獲得 AND 結果,即r.type LIKE 'Cafe' AND a.location LIKE 'Gulberg' AND r.cuisine_bkup LIKE 'Pizza'

使用%like

WHERE r.type LIKE '%Cafe%' 
AND a.location LIKE '%Gulberg%' 
AND  r.cuisine_bkup LIKE '%Pizza%'
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

要使 RIGHT join 工作,您必須將AND a.location LIKE 'Gulberg'移動到ON 。否則它將作為INNER JOIN起作用。

我想指出 '=' 的工作速度比LIKE快得多(如果您將字符等同)..

希望這可以幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM