MySQL使用IF函数来动态执行where条件

IF函数

IF(expression ,expr_true, expr_false);

MySQL的IF()函数,接受三个表达式,如果第一个表达式为true,而不是零且不为NULL,它将返回第二个表达式。否则,它返回第三个表达式。根据使用它的上下文,它返回数字或字符串值。

SELECT
	id,
	shop_uid,
	goods_name,
	goods_sn,
	is_show,
	source_type,
	is_on_sale,
	is_use_agency_region_limit 
FROM
	zsp_goods 
WHERE
IF
	( source_type = 1 AND is_sale_region = 1 AND is_use_agency_region_limit = 1, id = 33529, TRUE ) 
	AND goods_sn = 'H7011891' 
	AND shop_uid = 1 
	LIMIT 10

zed
请先登录后发表评论
  • latest comments
  • 总共0条评论