mysql 存储过程-字符串判断查询

来源:互联网 发布:网络违法犯罪举报电话 编辑:程序博客网 时间:2024/06/08 09:50

mysql if 用法,3个参数,如果第一个为true,则执行第二个参数,否则执行第三个参数

参数:IN shopUserName varchar(50),IN pageNo int,IN pageSize int   //请注意varchar(50) 长度

查询体:

BEGIN
SELECT sc.ShopId,sc.ShopState,sc.ExpireTime,sp.Username UserName,activit.ActivityCount ActivityCount 
from shopconfigs sc LEFT JOIN managers sp on sc.ShopId = sp.ShopId
        LEFT JOIN (SELECT ShopId,count(1) ActivityCount from activities where State<>0 GROUP BY ShopId) activit on activit.ShopId = sc.ShopId
        where 1=1 and IF(shopUserName <>'', sp.Username=shopUserName, '1=1') 
        ORDER BY sc.ShopId ASC  
        limit pageNo,pageSize ;
END



Like用法

BEGIN
SELECT sc.ShopId,sc.ShopState,sc.ExpireTime,sp.Username UserName,activit.ActivityCount ActivityCount 
from shopconfigs sc LEFT JOIN managers sp on sc.ShopId = sp.ShopId
        LEFT JOIN (SELECT ShopId,count(1) ActivityCount from activities where State<>0 GROUP BY ShopId) activit on activit.ShopId = sc.ShopId
        where 1=1 and IF(shopUserName <>'', sp.Username LIKE CONCAT('%',shopUserName,'%'), '1=1') 
        ORDER BY sc.ShopId ASC  
        limit pageNo,pageSize ;
END


0 1
原创粉丝点击