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

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                經常在SQL中會遇到比較復雜的SQL,很多老手也不知道怎么入手。在Postgres數據庫中有一種強大的with用法,可以分拆復雜的SQL并重新組裝,也可以當做臨時表來使用,之前也曾用with語法來實現SQL層面的遞歸。以下介紹開發中遇到的另一個例子。? 環境: Postgres 9.1.2? 評論表T1(user_id reference T2(user_id))? 用戶表T2(user_id)? **場景:**? 需要對T1表中數據按評論數分組排序,選擇前5條記錄與表T2進行關聯,返回滿足條件的T2用戶數據,但要根據T1的排序結果來展示,也就是要顯示評論最多的5個用戶的詳細信息,并按評論數把用戶從高到低排列。? 開發的SQL:? 1.查看表T1,并排序 ~~~ select user_id,count(1) from t1 group by user_id order by count(1) desc limit 5; ~~~ ??[![](http://static.oschina.net/uploads/space/2013/0120/154516_bhDU_267373.jpg)](http://static.oschina.net/uploads/space/2013/0120/154516_bhDU_267373.jpg)? 2.與表T2關聯 ~~~ select * from t2 where user_id in( select user_id from t1 group by user_id order by count(1) desc limit 5); ~~~ 但是展示的T2結果雖然取出來了,但并沒有排序,沒有達到效果? [![](http://static.oschina.net/uploads/space/2013/0120/154627_SSOx_267373.jpg)](http://static.oschina.net/uploads/space/2013/0120/154627_SSOx_267373.jpg)? **解決辦法:**? 1.使用兩個表的join來實現 ~~~ select t2.user_id,t2.user_name,count(1) as num from t1,t2 where t1.user_id = t2.user_id group by t2.user_id,t2.user_name order by num desc limit 5; ~~~ 2.用with來構造: ~~~ with tmp as( select user_id,count(1) as num from t1 group by user_id order by num desc limit 5 ) select t2.*,tmp.num from t2 inner join tmp on t2.user_id = tmp.user_id order by tmp.num desc ~~~ **分析:**? 第一種辦法需要兩張表關聯再分組取前5條,執行計劃如下: [![](http://static.oschina.net/uploads/space/2013/0120/162327_C84g_267373.jpg)](http://static.oschina.net/uploads/space/2013/0120/162327_C84g_267373.jpg) 如果數據量比較大,尤其是T2表很大的時候,會消耗比較多的資源,另外如果想取用戶表里有其他的字段,也需要進行分組 第二種辦法是采用with來構造臨時表,然后再去與T2表關聯取數,執行計劃如下:? [![](http://static.oschina.net/uploads/space/2013/0120/162339_fVTc_267373.jpg)](http://static.oschina.net/uploads/space/2013/0120/162339_fVTc_267373.jpg) 可以看到消耗的資源相對少一點,測試中發現T2、T1表很大的時候,這種差距尤其明顯。
                  <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>

                              哎呀哎呀视频在线观看