<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                [TOC] ## where 和 whereOr 聯合查詢 ### 1、實現多條件復合查詢 >[info] 這類查詢開發經常用到:先獲取到部分滿足條件記錄,再篩選出想要的結果 ### 2、實現手段: >[info] 因涉及到復雜查詢條件的生成,這非常適合閉包(匿名函數)來實現 ### 3、實例演示: #### 任務:查詢dept=1或者sex=1的員工中,id 在1010到1020之間的員工信息 >[info] 分析:這是在上節課whereOr實例基礎上,再增加一個AND條件 1.可能有學員會問,我們用where或whereOr連續調用行嗎?理論上似乎存在這種可能,實際上不能實現的,因為多個邏輯條件羅列在一起時,where或whereOr處理起來并不區別主次,全部依次執行,達不到我們要的效果。 * 我們修改一下Index.php: ~~~ <?php namespace app\index\controller; //導入數據庫類 use think\Db; class Index { public function index(){ //查詢 dept(部門)等于 1 的員工信息 $result = Db::table('tp5_staff') // 設置數據表 -> field('id,name,dept') // 設置結果集中允許顯示的字段 -> where('dept = 1') // 設置查詢條件dept =1 -> whereOr('sex = 1') // 設置查詢條件 sex = 1 邏輯OR關系 -> where('id between 1010 and 1020') //設置id > 1010 and id < 1020之間 -> select(); // 獲取結果集 //查看結果 dump($result); } } ~~~ * 生成的SQL: ~~~ SELECT `id`,`name`,`dept` FROM `tp5_staff` WHERE ( dept = 1 ) AND ( id between 1010 and 1020 ) OR ( sex = 1 ) ~~~ * 運行結果: ![](https://box.kancloud.cn/5bde9e39fc5164db5c89a0d7cf9715e8_962x775.png) >[danger] 顯然這個結果不是我想要的結果 >因此,二個以上的查詢條件、并且邏輯關系包括了AND和OR時,不用連接調用where或whereOr來實現。要用閉包(匿名函數)來做! #### 用閉包實現混合條件查詢,完成上面的實例: >[success] 思路:上面語句的錯誤在于前二個where和whereOr方法生成的查詢條件,沒有打包在一組中,我們用閉包來實現,最后一個where('id between 1010 and 1020')是不需要改造的。 * 用閉包再次修改一下Index.php: ~~~ <?php namespace app\index\controller; //導入數據庫類 use think\Db; class Index { public function index(){ //查詢 dept(部門)等于 1 的員工信息 $result = Db::table('tp5_staff') // 設置數據表 -> field('id,name,dept') // 設置結果集中允許顯示的字段 -> where(function($query){ $query -> where('dept = 1') -> whereOr('sex = 1') ; }) -> where('id between 1010 and 1020') //設置id > 1010 and id < 1020之間 -> select(); // 獲取結果集 //查看結果 dump($result); } } ~~~ * 查看結果: ~~~ array(3) { [0] => array(3) { ["id"] => int(1010) ["name"] => string(9) "歐陽峰" ["dept"] => int(2) } [1] => array(3) { ["id"] => int(1011) ["name"] => string(9) "李云龍" ["dept"] => int(1) } [2] => array(3) { ["id"] => int(1012) ["name"] => string(9) "楚云飛" ["dept"] => int(3) } } ~~~ * 生成的SQL語句: >[warning] 現在是正確的啦,剛才生成的SQL語句是不對的 ~~~ SELECT `id`,`name`,`dept` FROM `tp5_staff` WHERE ( ( dept = 1 ) OR ( sex = 1 ) ) AND ( id between 1010 and 1020 ); ~~~ * 將該語句放在SQLPRO 中執行: ![](https://box.kancloud.cn/164f79196669e672afdce59a7de22eaf_835x306.png) ### 4、總結: #### 復雜條件查詢,用閉包方式,將多個條件打包是不錯的方法,一定要熟練掌握
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看