SELECT * FROM mm_tfs
WHERE product_slug LIKE '%football%'
AND schoolid = '8' AND category_id ='21'
LIMIT 4
返回4个值,就像我问的那样,但是下面的语句返回0 - 是否有关于使用我不熟悉的OR语句的规则?我的假设是它应该返回1中的所有值(如果不是限制,则返回更多值)。
SELECT * FROM mm_tfs
WHERE (product_slug LIKE '%football%'
AND schoolid = '8' AND category_id ='21')
OR (product_description LIKE '%football%'
AND schoolid = '8' AND category_id ='21')
LIMIT 4
cyberkiwi注意 Q2的第一个OR部分与Q1上的WHERE子句完全相同
product_description LIKE '%football%' AND schoolid = '8' AND category_id ='21
没有OR语句本身只要没有限制就会产生所需的结果。当OR语句与LIMIT一起使用时,返回0值。
SELECT * FROM mm_tfs
WHERE product_description LIKE '%football%'
AND schoolid = '8' AND category_id ='21'
LIMIT 4
^ - 产生0结果
SELECT * FROM mm_tfs
WHERE product_description LIKE '%football%'
AND schoolid = '8' AND category_id ='21'
^ - 产生结果
最奇怪的是,所有这些查询在我的PHPMYADMIN SQL查询窗口中产生了正确的效果,但在应用程序本身中却没有。
答案 0 :(得分:1)
尝试将()放在整个条件
( (...AND...) OR (...AND...) )
像这样:
SELECT * FROM mm_tfs WHERE
( (product_slug LIKE '%football%' AND schoolid = '8' AND category_id ='21')
OR (product_description LIKE '%football%' AND schoolid = '8' AND category_id
='21')
) LIMIT 4
答案 1 :(得分:1)
您正在重复一些不必要的条件。试试这个:
SELECT * FROM mm_tfs
WHERE
(product_slug LIKE '%football%'
OR product_description LIKE '%football%')
AND schoolid = '8'
AND category_id ='21'
LIMIT 4
<强>更新强>
我创建了下表:
create table mm_tfs2 (schoolid varchar(2), categoryid varchar(2),
description varchar(20), slug varchar(20));
并且5次:
insert into mm_tfs2 values (8, 21, '', 'football');
最后查询:
select * from mm_tfs2 where
(slug like '%football%' and schoolid = 8 and categoryid = 21)
or (description like '%football%' and schoolid = 8 and categoryid = 21)
limit 4;
+----------+------------+-------------+----------+
| schoolid | categoryid | description | slug |
+----------+------------+-------------+----------+
| 8 | 21 | | football |
| 8 | 21 | | football |
| 8 | 21 | | football |
| 8 | 21 | | football |
+----------+------------+-------------+----------+
4 rows in set (0.00 sec)
所以我很遗憾地说我无法重现这个问题。
答案 2 :(得分:1)
我认为你可以清理一下,你只能获得一所学校的行和一个类别ID,所以你没有理由要检查这两个时间:
SELECT *
FROM mm_tfs
WHERE schoolid = '8'
AND category_id ='21'
AND (product_slug LIKE '%football%'
OR product_description LIKE '%football%')
LIMIT 4;