格式:IF(<condition>,<value if true>,<value if false>)
释义:如果condition为true,则执行前面的条件;condition为false,则执行后面的条件
示例:
查找名字为张三并且是地区是湖南,和名字不是张三的所有用户:
SELECT * FROM USER WHERE IF( NAME ='张三',address LIKE '%湖南%',1=1 )
简单的写法:
SELECT * FROM USER WHERE 1 = 1 AND NAME != '张三' OR (NAME = '张三' AND address LIKE '%湖南%' )
也可以这么写:
SELECT * FROM USER WHERE 1 = 1 AND NAME != '张三' OR address LIKE '%湖南%'
项目代码:
查询s3为null的全部记录和s3不为null,而且p.display_code 在s3里面的记录
WHERE 1 = 1 AND s1 = '5' AND IF( s3 IS NULL,TRUE,FIND_IN_SET(p.display_code, s3))