您的位置:首页 > 数据库

关于sql语句中最后where 1=1

2015-07-02 16:47 323 查看
看似不重要的一句话其实可以省略好多代码

例如String sql=" select * from student where 1=1";

if (name!=null)  sql+="and name like "%%"";

if (age!=null)  sql+="and
age like "%%"";

如果没有使用where 1=1;

String sql=" select * from student";

if (name!=null)  sql+="where name like "%%"";

if(name==null and age!=null)
sql+="where age like "%%"";

if (age!=null)  sql+="and
age like "%%"";

两个字段的非空可能都要判断的,逻辑思维比较杂乱,所以用上where 1=1 就可以解决很多问题的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: