MYSQL SELECT子句给出了不需要的结果。多个WHERE AND语句

时间:2013-10-09 11:58:31

标签: mysql sql inner-join where-clause

SELECT `post`.*, `user_account`.`project_name` 
FROM 
`post` 
INNER JOIN `user_account` ON `post`.`user_id` = `user_account`.`user_id` 
WHERE 
`post`.`deleted_at` IS NULL 
AND `is_active` = 1 
AND `deleted_at` IS NULL 
AND `end` > '2013-10-09' 
AND `user_account`.`project_name` LIKE '%intensiivkursus%' 
OR ( 
`title` LIKE '%intensiivkursus%' 
OR `topics` LIKE '%intensiivkursus%' 
OR `target_group` LIKE '%intensiivkursus%' 
OR `lectors` LIKE '%intensiivkursus%' 
OR `timetable` LIKE '%intensiivkursus%' 
) 
GROUP BY 
`post`.`id` 
ORDER BY 
`post`.`created_at` DESC

我希望这只能用is_active = 1给出结果,但由于某种原因,它会产生相反的结果。我得到了5个结果,全都是is_active = 0
我在这里缺少什么?

1 个答案:

答案 0 :(得分:0)

我怀疑你的OR / AND和括号有问题。运营商的优先权令人困惑。

你不是说

AND 
(`user_account`.`project_name` LIKE '%intensiivkursus%' 
OR `title` LIKE '%intensiivkursus%' 
OR `topics` LIKE '%intensiivkursus%' 
OR `target_group` LIKE '%intensiivkursus%' 
OR `lectors` LIKE '%intensiivkursus%' 
OR `timetable` LIKE '%intensiivkursus%' 
) 

或者

AND `user_account`.`project_name` LIKE '%intensiivkursus%' 
AND ( 
`title` LIKE '%intensiivkursus%' 
OR `topics` LIKE '%intensiivkursus%' 
OR `target_group` LIKE '%intensiivkursus%' 
OR `lectors` LIKE '%intensiivkursus%' 
OR `timetable` LIKE '%intensiivkursus%' 
)