亚洲最大看欧美片,亚洲图揄拍自拍另类图片,欧美精品v国产精品v呦,日本在线精品视频免费

  • 站長資訊網(wǎng)
    最全最豐富的資訊網(wǎng)站

    SQL中“where 1=1”有什么用?

    SQL中“where 1=1”有什么用?

    SQL中where 1=1的用處

    解釋:

    其實,1=1 是永恒成立的,意思無條件的,也就是說在SQL語句中有沒有這個1=1都可以。

    這個1=1常用于應(yīng)用程序根據(jù)用戶選擇項的不同拼湊where條件時用的。

    如:web界面查詢用戶的信息,where默認(rèn)為1=1,這樣用戶即使不選擇任何條件,sql查詢也不會出錯。如果用戶選擇了姓名,那么where變成了where 1=1 and 姓名=‘用戶輸入的姓名’,如果還選擇了其他的條件,就不斷在where 條件后追加 and語句就行了。

    如果不用1=1的話,每加一個條件,都要判斷前面有沒有where 條件,如果沒有就寫where …,有就寫and語句,因此此時用1=1可以簡化了應(yīng)用程序的復(fù)雜度。

    例:

    如下面代碼首先定義$where= ‘1=1’,后面就可以不用去判斷是否存在$where

    public function listAction()     {        $get = $this->getQuery();         $statementBalanceDetailModel = M('TicketStatementBalanceDetail');          $page = isset($get['page']) ? intval($get['page']) : 1;         $pageSize = isset($get['page_size']) ? intval($get['page_size']) : 10;          //用處         $where = ' 1=1 ';         $binds = array();         if (isset($get['id']) && $get['id'] != '') {             $where .= ' and id = :id';             $binds['id'] = trim($get['id']);         }          if (isset($get['shop_name']) && $get['shop_name'] != '') {             $where .= ' and shop_name = :shop_name';             $binds['shop_name'] = trim($get['shop_name']);         }          if (isset($get['statement_sn']) && $get['statement_sn'] != '') {             $where .= ' and statement_sn = :statement_sn';             $binds['statement_sn'] = trim($get['statement_sn']);         }          $where .= ' order by id desc';         $result = $statementBalanceDetailModel->paginate($where, $pageSize, $page, $fields = array(), $binds);         $sceneryList = $result['data'];         $total = $result['total_result'];         $pager = Paginate::web($total, $page, $pageSize);          $data = array(             'pager' => $pager,             'sceneryList' => $sceneryList,         );          $this->getView()->assign($data);     }

    本文轉(zhuǎn)載自:https://blog.csdn.net/longgeaisisi/article/details/100599010

    推薦學(xué)習(xí):mysql教程

    贊(0)
    分享到: 更多 (0)
    網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號