这个查询应该如何?

时间:2014-10-08 17:30:01

标签: mysql mysqli

我对此查询有疑问:

SELECT * 
FROM posts 
WHERE title LIKE '%samsung%' 
OR title LIKE '%42%' 
AND price >= '100' 
AND price <= '1000000' 
AND shipping = '1' 
AND sale_type = '1' 
LIMIT 0,50

问题在于,当我使用2个或更多title LIKE '%%'运行查询时,它只向我显示title LIKE '%%'

的查询
SELECT * 
FROM posts 
WHERE title LIKE '%samsung%' 
AND price >= '100' 
AND price <= '1000000' 
AND shipping = '1' 
AND sale_type = '1'
LIMIT 0,50

忽略其他选项。但是,如果我只使用标题LIKE&#39; %%&#39;运行查询它很完美。

希望有人能帮我一臂之力

3 个答案:

答案 0 :(得分:2)

使用括号:

SELECT * FROM posts 
WHERE (title LIKE '%samsung%' OR title LIKE '%42%')
    AND price >= '100' 
    AND price <= '1000000' 
    AND shipping = '1' 
    AND sale_type = '1' 
LIMIT 0,50

答案 1 :(得分:2)

您的查询应该是

SELECT * FROM posts 
WHERE price >= '100' 
AND price <= '1000000' 
AND shipping = '1' 
AND sale_type = '1'
AND (title LIKE '%samsung%' OR title LIKE '%42%') 
LIMIT 0,50

答案 2 :(得分:2)

您可以尝试这种方式:

WHERE title REGEXP 'samsung|42'

此处有更多信息:http://forums.mysql.com/read.php?10,392332,392950#msg-392950