MySQL中变量为空时不执行where过滤

set @Location_Id = 'a';
set @District_Id = 'b';
set @Category_Id = 'c';

select * 
from table_name
where 1=1 and
if(@Location_Id = '', 0=0, LocationId = @Location_Id)  and
if(@District_Id = '', 0=0, DistrictId = @District_Id)  and
if(@Category_Id = '', 0=0, CategoryId = @Category_Id);

 

posted @ 2020-07-09 18:00  无敌师爷IT技术Blog  阅读(633)  评论(0编辑  收藏  举报