我想实现一个查询,sql语句如下
select * from user where ( id like '%1%' or name like '%foo%') and active=1
其中id,name可以是动态的,可以为null
我写的是这样的
可是当id为空的时候就有错误:
select * from user where ( or name like '%foo%' ) and active =1
因为加了括号
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
可以使用<trim>标签
最简单的办法,就是"("的后面加上1=0
不过像@毛宇鹏V说的用
trim也是可以实现的,而且更加合理