<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          年輕人不講武德,where 1=1 是什么鬼?

          共 1041字,需瀏覽 3分鐘

           ·

          2020-12-04 14:47

          來源:cloud.tencent.com/developer/article/1475146

          這段代碼應該是由程序(例如Java)中生成的,where條件中 1=1 之后的條件是通過 if 塊動態(tài)變化的。例如:

          String?sql="select?*?from?table_name?where?1=1";??
          ??
          if(?conditon?1)?{??
          ??
          ??sql=sql+"??and??var2=value2";??
          ??
          }??
          ??
          if(conditon?2)?{??
          ??
          ??sql=sql+"??and?var3=value3";??
          ??
          }??
          ?

          where 1=1 是為了避免where 關(guān)鍵字后面的第一個詞直接就是 “and”而導致語法錯誤。

          動態(tài)SQL中連接AND條件

          where 1=1 是為了避免where 關(guān)鍵字后面的第一個詞直接就是 “and”而導致語法錯誤。

          where后面總要有語句,加上了1=1后就可以保證語法不會出錯!

          select * from table where 1=1

          因為table中根本就沒有名稱為1的字段,所以該SQL等效于select * from table,

          這個SQL語句很明顯是全表掃描,需要大量的IO操作,數(shù)據(jù)量越大越慢,

          建議查詢時增加必輸項,即where 1=1后面追加一些常用的必選條件,并且將這些必選條件建立適當?shù)乃饕?,效率會大大提?/p>

          「拷貝表」

          create table table_name ?as ?select ?* ?from ?Source_table ?where ?1=1;

          「復制表結(jié)構(gòu)」

          create table table_name ?as ?select ?* ?from ?Source_table ?where ?1 <> 1;

          長按關(guān)注,學習更多

          推薦閱讀

          瀏覽 52
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  男人天堂av网 | 亚洲五月天堂 | 露脸露逼 | 国产成人精品一区二区三区四区五区 | 人妻夜夜爽天天爽三区麻豆AV网站 |