zoukankan      html  css  js  c++  java
  • sql中where 1=1 的使用

    where 1=1; 这个条件始终为True,在不定数量查询条件情况下,1=1可以很方便的规范语句。

    例如为不定数量的查询条件,我们在后台写查询的时候,类似于这样的语句 string sql ="select * from table where"

    if(starttime!=null){

    sql =sql+" starttime="+starttime

    if(endtime !=null){

    sql =sql+"and endtime ="+endtime

    }

    这时我们的查询语句就是 select * from table where starttime =2015-04-05 and endtime = 2015-04-07,查询语句正确

    但是如果条件都不满足的话,语句就变成了 select * from table where ,这时候查询就会报错,

    加上1=1的时候

    string sql ="select * from table where 1=1",

    if(starttime!=null){

    sql =sql+" and  starttime="+starttime

    if(endtime !=null){

    sql =sql+"and endtime ="+endtime

    }

    当两个条件成立的时候 select * from table where 1=1 and starttime =2015-04-05 and endtime = 2015-04-07, 语句正确

    当两个条件不满足时 select * from table where 1=1 ,语句正确,会返回table表的所有数据

  • 相关阅读:
    Vue props向子组件中传递数据
    Vue 组件间的通信
    vue slot插槽
    Vue 组件化注意事项
    VUE多个组件示例
    Vue组件化开发
    Vue 获取当前时间并格式化
    VUE 过滤器以及插件
    Vue 表单数据双向绑定 v-mode
    VUE 事件修饰符以及按键码
  • 原文地址:https://www.cnblogs.com/wangcongsuibi/p/7813445.html
Copyright © 2011-2022 走看看