您好我从以下逻辑制作MySQL声明。
Select * from content
if(description=='educational')
where serial like '%EDU2015%'
else where serial like '%NOVEL2015%'
逻辑很简单,如果描述列具有教育性,则过滤器应该具有字符串" EDU2015。否则,它应该使用" NOVEL2015"
的序列进行过滤我不确定我是否使用了在mysql中使用IF语句的方法,但我不知道从https://dev.mysql.com/doc/refman/5.7/en/if.html
那里放置where语句的位置答案 0 :(得分:5)
您可以像这样使用CASE表达式:
SELECT * FROM content
WHERE serial like CASE WHEN description = 'educational'
THEN '%EDU2015%'
ELSE '%NOVEL2015%'
END
请注意,比较值时,需要一个等号,而不是两个。
编辑:如果要根据条件筛选不同的列,可以使用:
SELECT * FROM content
WHERE (description = 'educational' and serial like '%EDU2015%')
OR(description <> 'educational' and id>100)
答案 1 :(得分:5)
您可以在WHERE块中使用IF
,如下所示:
Select *
from content
where serial like IF(description = 'educational','%EDU2015%','%NOVEL2015%');
或者,您可以选择像@sagi所述的CASE WHEN
表达式。
mysql> SELECT IF(1>3,'true','false');
+------------------------+
| IF(1>3,'true','false') |
+------------------------+
| false |
+------------------------+
1 row in set (0.00 sec)