关于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 就可以解决很多问题的。
例如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 就可以解决很多问题的。
相关文章推荐
- MySQL相关问题
- redis 集群
- RedHat6下源码安装MySQL5.6
- ORA-01219:数据库未打开:仅允许在固定表/视图中查询
- 【Oracle错误集锦】:ORA-00119 & ORA-00132
- 清理mysql的sleep链接
- OEM无法登陆原因与ORACLE用户密码过期和锁定的解决办法(附上系统用户默认密码)
- mysql修改主键
- oracle分区表彻底删除的办法
- redis 集群
- 用Redis实现微博关注关系
- Real-Time SQL Monitoring详解
- 九、oracle 事务
- Vertica数据库操作
- sql server 2012 数据引擎任务调度算法解析(上)
- myawr : mysql性能监控
- ORA-01109:数据库未打开(解决)
- Oracle中Hint深入理解
- 60. C# -- 读取 appconfig文件配置数据库连接的方法
- MongoDB安装篇-Win7 X64