<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] ## having方法 ### 1、對分組統計的結果,進行篩選 >[info] 如果將分分組查詢的結果看成一張表的話,having方法類似where語句的功能 ### 2、源碼:/thinkphp/library/think/db/Query.php ~~~ /** * 指定having查詢 * @access public * @param string $having having * @return $this */ public function having($having) { $this->options['having'] = $having; return $this; } ~~~ >[info] 源碼非常簡單,就是給Query類的查詢選項屬性options[ 'having' ] 賦值 ### 3、參數與返回值 * 參數 >[info] 如果把分組查詢結果看作一張新數據表的話,having就相當于該表的where操作。同理,這個“新表”中應該只有分組字段和統計字段(由聚合函數計算得到的值組成的字段)。我們having方法,就是在統計字段中,找出符合條件的記錄:如平均值大于500之類的條件。 | 序號 | 參數 | 說明 | | :---: | --- | --- | | 1 | 聚合函數組成的運算表達式 | 從統計查詢結果是,篩選出符合條件的記錄 | >[success] having方法與where方法都可以設置查詢條件,但二者使用是有區別的: > (1) where方法中的字段,必須是表中實際真實存在的字段 > (2) having方法中的定段,不一定是表中實際字段,只要是select語句可以查詢或計算出來的表達式都可以,例如拼接字段名、數值型字段的算術運算、統計類聚合函數等;換句話說,having 中的字段,必須在select后面的字段列表中出現。 * 返回值:與其它方法一樣,返回查詢對象本身,便于后面訪問調用 ### 4、實例演示 * 任務1:查詢tp5_staff表中,各部門的平均工資大于6400元的員工信息 >[info] 該實例是前面group方法的加強版,加了一個限定條件而已,請對照著看 * Index.php 控制器中代碼如下: ~~~ <?php namespace app\index\controller; //導入數據庫類 use think\Db; class Index { public function index(){ //查詢部門的平均工資,小數保留2位 $result = Db::table('tp5_staff') //字段名稱中,可以使用聚合統計函數 -> field(['dept'=>'部門','ROUND(AVG(salary),2)'=>'平均工資']) -> group('dept') -> having('avg(salary) > 6400') -> select(); //查看結果 dump($result); } ~~~ * 查詢結果如下: ~~~ array(2) { [0] => array(2) { ["部門"] => string(9) "市場部" ["平均工資"] => float(9431.33) } [1] => array(2) { ["部門"] => string(9) "開發部" ["平均工資"] => float(6456.43) } } ~~~ >[warning] 可以看到,原來的市場部平均工資數據消失了,因為他的平均工資不到6400 * 生成的SQL語句如下: ~~~ SELECT `dept` AS `部門`,ROUND(AVG(salary),2) AS `平均工資` FROM `tp5_staff` GROUP BY dept HAVING avg(salary) > 6400 ~~~ * 表中數據如下: ![](https://box.kancloud.cn/50e6d22202c2d59c84b3032b67cd315b_860x331.png) ### 總結: >[warning] having條件與where條件非常相似,但又有本質不同。where是根據某列特征進行查詢,而having是在查詢已經產生的結果集中進行篩選。這二個方法執行的對象和時機是不同的,請注意區別。
                  <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>

                              哎呀哎呀视频在线观看