<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>

          SQL 語句中 where 條件后 寫上 1=1 是什么意思!

          共 859字,需瀏覽 2分鐘

           ·

          2021-02-04 09:50


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

          這段代碼應(yīng)該是由程序(例如Java)中生成的,where條件中 1=1 之后的條件是通過 if 塊動(dòng)態(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)鍵字后面的第一個(gè)詞直接就是 “and”而導(dǎo)致語法錯(cuò)誤。

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

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

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

          select?*?from?table?where?1=1

          因?yàn)閠able中根本就沒有名稱為1的字段,所以該SQL等效于select * from table,

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

          建議查詢時(shí)增加必輸項(xiàng),即where 1=1后面追加一些常用的必選條件,并且將這些必選條件建立適當(dāng)?shù)乃饕?,效率?huì)大大提高

          拷貝表

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

          復(fù)制表結(jié)構(gòu)

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

          END




          ↓↓↓↓點(diǎn)擊下方獲取源碼和教程↓↓↓↓

          瀏覽 153
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <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>
                  亚洲成人一区二区三区 | 欧美色就是色操逼视频 | 91人爽 | 大屌成人免费视频 | sese99sese |