mysql and与or连用时遇到的坑

1,

SELECT *

FROM work_log_bbs_up

WHERE TYPE IS NULL OR TYPE = 0 OR TYPE = 1 OR TYPE = 2 AND is_del = 0

结果:1166

 

2,

SELECT *

FROM work_log_bbs_up

WHERE (TYPE IS NULL OR TYPE = 0 OR TYPE = 1 OR TYPE = 2) AND is_del = 0

结果:492

 

原因:mysql and 优先级比or高,sql 1 会首先计算TYPE = 2 AND is_del = 0然后与其他条件进行or比较。需要使用()包围起来,明确优先级,因为()优先级比and高。

posted @ 2020-09-15 15:06  梦飞翔up  阅读(275)  评论(0编辑  收藏  举报