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

最近看到很多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之外的其它永真永假的條件同理。

? 版權聲明
THE END
喜歡就支持一下吧
點贊15 分享