AND列在多列上的WHERE子句中

时间:2015-10-29 12:32:03

标签: mysql

我有两张桌子

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条件

3 个答案:

答案 0 :(得分: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 而不是 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

另请注意,只有当所有三个条件都为TRUE即r.type LIKE 'Cafe' AND a.location LIKE 'Gulberg' AND r.cuisine_bkup LIKE 'Pizza' )时,您才能获得AND的结果

答案 1 :(得分:0)

%like

一起使用
WHERE r.type LIKE '%Cafe%' 
AND a.location LIKE '%Gulberg%' 
AND  r.cuisine_bkup LIKE '%Pizza%'

答案 2 :(得分: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

要使RIGHT加入工作,您必须将AND a.location LIKE 'Gulberg'移至ON部分。否则,它将作为INNER JOIN

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

希望这有帮助