我有来自MySQL表的请求。它看起来如下:
SELECT s . * , if( v.ip IS NULL , 0, 1 ) AS voted, v.vote
FROM `emvc4_records` s
LEFT JOIN `emvc4_records_votes` v ON ( s.id = v.record_id
AND v.day = CURRENT_DATE
AND v.ip = '2130706433' )
WHERE 1 =1
AND `category` =2
AND `caption` LIKE '%My%'
OR `description` LIKE '%My%'
ORDER BY `rating` DESC
LIMIT 0 , 25
我得到了这个结果:
id category category_name caption description created postby votes_up votes_down rating status have_voted vote
5 1 My second idea My second idea 2013-06-04 00:00:00 102 2 0 2 0 0 NULL
7 2 My first question My first question 2013-06-04 00:00:00 102 2 0 2 0 0 NULL
9 2 My second My second question 2013-06-04 00:00:00 102 1 0 1 0 0 NULL
我的问题:在我的SQL查询中,我有:“AND category
= 2”。那么为什么我在输出中得到记录id = 5(它有category = 1)?
谢谢!
答案 0 :(得分:1)
WHERE
子句中的条件未正确组织。添加括号以对所需条件进行分组。我想LIKE '%My%'
应该分组。
SELECT s . *
, if( v.ip IS NULL , 0, 1 ) AS voted
, v.vote
FROM `emvc4_records` s
LEFT JOIN `emvc4_records_votes` v ON ( s.id = v.record_id
AND v.day = CURRENT_DATE
AND v.ip = '2130706433' )
WHERE 1 = 1
AND `category` = 2
AND (`caption` LIKE '%My%' OR `description` LIKE '%My%')
ORDER BY `rating` DESC
LIMIT 0 , 25
我不是您添加1 = 1
的原因,但您可以删除它。
答案 1 :(得分:0)
您的查询应如下所示:
...............
`category` =2
AND (`caption` LIKE '%My%' OR `description` LIKE '%My%')
.................