SQL 的 1=1 与 1<> 1

1.1=1

sql 中的查询语句中的where 字句是为了带条件进行查询,那么使用where 1=1

select * from test where 1=1

因为  1=1 表示永远为真,不起约束作用查询所有,不会报错

在动态查询的时候会使用

如多条件查询:

select * from test where 1=1 and a=? and b like .. and c like ...

当后面的几个条件都为空时,就只剩下1=1永真,将所有的结果返回

 

2.1<>1

select * from test where 1<>1

等同于select * from test where false,将返回空结果集

当我们需要获取表的字段(结构)信息,而不需要理会实际保存的记录时,可以使用where 1<>1

posted @ 2022-03-11 21:47  durtime  阅读(398)  评论(0)    收藏  举报