SQL中的 where 1= 1?以及select 1 from table?意义!

来源:互联网 发布:网络兼职骗局信誉代刷 编辑:程序博客网 时间:2024/05/01 08:51



SQL语句中的 where 1=1是什么意思?

就是条件永远为真,查出所有数据来

在组合查询条件时候多用: 

String sql="select * from user where 1=1 "; 

if(username!=null) sql=sql+ " and username='"+username+"'"; 

if(password!=null) sql=sql+ " and password='"+password+"'"; 

这样方便很多,及时username,password两者都为空都可以查询

永远为真 

主要是为了便于动态连接后续条件

有时候想查看一下表的字段就 
select * from UB where 1=2,表示false 1=1表示true

-------------------------------------------

package StringTest;

public class Where {

    /**
     * 我用于高级查询情况下
     * @param args
     */
    public static void main(String[] args) {
        Where s= new Where();
        s.print("13432134321", "www.cjfuture.cn");
        //null时,即为用户没有写值
        s.print(null, "www.cjfuture.cn");
    }
    public void print(String mobile,String url){
        StringBuffer sb = new StringBuffer("select id from tab where 1 = 1 ");
        //如果没有1=1,下面的判断语句会比现在写复杂
        if(mobile!=null){
            sb.append("and mobile = "+mobile);
        }
        if(url!=null){
            sb.append("and name = "+url);
        }
        System.out.println(sb.toString());
    }
    
    public void otherPrint(String mobile,String url){
        StringBuffer sb = new StringBuffer("select id from tab");
        //如果没有1=1,下面的判断语句会比现在写复杂,where放在哪里合适?
        if(mobile!=null){
            sb.append("and mobile = "+mobile);
        }else{
            sb.append("");        //如何写?
        }
        if(url!=null){
            sb.append("and name = "+url);
        }else{
            sb.append("");        //如何写?
        }
        System.out.println(sb.toString());
    }
}



table表是一个数据表,假设表的行数为10行。

1:select  1 from table       增加临时列,每行的列值是写在select后的数,这条sql语句中是1

2:select count(1)  from table   管count(a)的a值如何变化,得出的值总是table表的行数

3:select sum(1) from table   计算临时列的和

 

 在SQL SERVER中用 1 测试了一下,发现结果如下:

1:测试结果,得出一个行数和table表行数一样的临时列(暂且这么叫,我也不知道该叫什么),每行的列值是1;

2:得出一个数,该数是table表的行数;

3:得出一个数,该数是table表的行数;

然后我又用“2”测试,结果如下:

1:得出一个行数和table表行数一样的临时列,每行的列值是2;

2:得出一个数,该数是table表的行数;

3:得出一个数,该数是table表的行数×2的数

然后我又用更大的数测试:

1:得出一个行数和table表行数一样的临时列,每行的列值是我写在select后的数;

2:还是得出一个数,该数是table表的行数;

3:得出一个数,该数是table表的行数×写在select后的数

    综上所述:第一种的写法是增加临时列,每行的列值是写在select后的数;第二种是不管count(a)的a值如何变化,得出的值总是table表的行数;第三种是计算临时列的和
 

语句if not exists(select 1 from deleted d join inserted i on d.ID=i.ID 
and d.col1=1 and i.col1=2)

if not exists 如果不存在 
(..)这里该是验证更新,更新前的id等于更新后的id,并且更新前col的值是1,更新后的值是2



SQL语句中的 where 1=1啥意思啊?

就是条件永远为真,查出所有数据来

在组合查询条件时候多用: 

String sql="select * from user where 1=1 "; 

if(username!=null) sql=sql+ " and username='"+username+"'"; 

if(password!=null) sql=sql+ " and password='"+password+"'"; 

这样方便很多,及时username,password两者都为空都可以查询

永远为真 

主要是为了便于动态连接后续条件

有时候想查看一下表的字段就 
select * from UB where 1=2,表示false 1=1表示true

-------------------------------------------

package StringTest;

public class Where {

    /**
     * 我用于高级查询情况下
     * @param args
     */
    public static void main(String[] args) {
        Where s= new Where();
        s.print("13432134321", "www.cjfuture.cn");
        //null时,即为用户没有写值
        s.print(null, "www.cjfuture.cn");
    }
    public void print(String mobile,String url){
        StringBuffer sb = new StringBuffer("select id from tab where 1 = 1 ");
        //如果没有1=1,下面的判断语句会比现在写复杂
        if(mobile!=null){
            sb.append("and mobile = "+mobile);
        }
        if(url!=null){
            sb.append("and name = "+url);
        }
        System.out.println(sb.toString());
    }
    
    public void otherPrint(String mobile,String url){
        StringBuffer sb = new StringBuffer("select id from tab");
        //如果没有1=1,下面的判断语句会比现在写复杂,where放在哪里合适?
        if(mobile!=null){
            sb.append("and mobile = "+mobile);
        }else{
            sb.append("");        //如何写?
        }
        if(url!=null){
            sb.append("and name = "+url);
        }else{
            sb.append("");        //如何写?
        }
        System.out.println(sb.toString());
    }
}

0 0
原创粉丝点击