Home  >  Article  >  Database  >  MYSQL where 1=1判定中的作用说明

MYSQL where 1=1判定中的作用说明

WBOY
WBOYOriginal
2016-06-07 18:05:30932browse

最近看到很多sql里用到where 1=1,原来觉得这没用嘛,但是又想到如果没用为什么要写呢?

于是在网上

查了查,在这里就浅谈一下:
1=1 永真, 11 永假。

11 的用处:
用于只取结构不取数据的场合
例如:
create table table_temp tablespace tbs_temp as
select * from table_ori where 11
建成一个与table_ori 结构相同的表table_temp,但是不要table_ori 里的数据。(除了表结构,其它结

构也同理)

1=1的用处
用于动态SQL
例如 lv_string := ‘select tbl_name,tbl_desc from tbl_test where 1=1 ‘||l_condition;
当用户选择了查询的名称'abc'时l_condition :='and tbl_name = ”abc””;但是当用户没有

选择名称查询时l_condition就为空 这样 lv_string = ‘select tbl_name,tbl_desc from tbl_test

where 1=1 ‘ ,运行也不会出错,相当于没有限制名称条件。但是如果没有1=1的条件,则lv_string =

‘select tbl_name,tbl_desc from tbl_test where ‘;这样就会报错。

除了1=1 或11之外的其它永真永假的条件同理。
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn