久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

  • <small id='TJdaE'></small><noframes id='TJdaE'>

  • <legend id='TJdaE'><style id='TJdaE'><dir id='TJdaE'><q id='TJdaE'></q></dir></style></legend>
      • <bdo id='TJdaE'></bdo><ul id='TJdaE'></ul>
      <tfoot id='TJdaE'></tfoot>

        <i id='TJdaE'><tr id='TJdaE'><dt id='TJdaE'><q id='TJdaE'><span id='TJdaE'><b id='TJdaE'><form id='TJdaE'><ins id='TJdaE'></ins><ul id='TJdaE'></ul><sub id='TJdaE'></sub></form><legend id='TJdaE'></legend><bdo id='TJdaE'><pre id='TJdaE'><center id='TJdaE'></center></pre></bdo></b><th id='TJdaE'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='TJdaE'><tfoot id='TJdaE'></tfoot><dl id='TJdaE'><fieldset id='TJdaE'></fieldset></dl></div>

        MySQL order by before group by

        MySQL order by before group by(MySQL order by before group by)

        <small id='VykMd'></small><noframes id='VykMd'>

          • <tfoot id='VykMd'></tfoot>
            <legend id='VykMd'><style id='VykMd'><dir id='VykMd'><q id='VykMd'></q></dir></style></legend>
              <tbody id='VykMd'></tbody>

            • <bdo id='VykMd'></bdo><ul id='VykMd'></ul>
                  <i id='VykMd'><tr id='VykMd'><dt id='VykMd'><q id='VykMd'><span id='VykMd'><b id='VykMd'><form id='VykMd'><ins id='VykMd'></ins><ul id='VykMd'></ul><sub id='VykMd'></sub></form><legend id='VykMd'></legend><bdo id='VykMd'><pre id='VykMd'><center id='VykMd'></center></pre></bdo></b><th id='VykMd'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='VykMd'><tfoot id='VykMd'></tfoot><dl id='VykMd'><fieldset id='VykMd'></fieldset></dl></div>
                  本文介紹了MySQL order by before group by的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  這里有很多類似的問題,但我認為沒有人能充分回答這個問題.

                  There are plenty of similar questions to be found on here but I don't think that any answer the question adequately.

                  我將從當前最流行的問題繼續,并使用他們的例子,如果沒關系.

                  I'll continue from the current most popular question and use their example if that's alright.

                  此實例中的任務是獲取數據庫中每個作者的最新帖子.

                  The task in this instance is to get the latest post for each author in the database.

                  示例查詢產生了無法使用的結果,因為它并不總是返回最新的帖子.

                  The example query produces unusable results as its not always the latest post that is returned.

                  SELECT wp_posts.* FROM wp_posts
                      WHERE wp_posts.post_status='publish'
                      AND wp_posts.post_type='post'
                      GROUP BY wp_posts.post_author           
                      ORDER BY wp_posts.post_date DESC
                  

                  當前接受的答案是

                  SELECT
                      wp_posts.*
                  FROM wp_posts
                  WHERE
                      wp_posts.post_status='publish'
                      AND wp_posts.post_type='post'
                  GROUP BY wp_posts.post_author
                  HAVING wp_posts.post_date = MAX(wp_posts.post_date) <- ONLY THE LAST POST FOR EACH AUTHOR
                  ORDER BY wp_posts.post_date DESC
                  

                  不幸的是,這個答案是簡單明了的錯誤,在許多情況下產生的結果不如原始查詢穩定.

                  Unfortunately this answer is plain and simple wrong and in many cases produces less stable results than the orginal query.

                  我最好的解決方案是使用表單的子查詢

                  My best solution is to use a subquery of the form

                  SELECT wp_posts.* FROM 
                  (
                      SELECT * 
                      FROM wp_posts
                      ORDER BY wp_posts.post_date DESC
                  ) AS wp_posts
                  WHERE wp_posts.post_status='publish'
                  AND wp_posts.post_type='post'
                  GROUP BY wp_posts.post_author 
                  

                  我的問題很簡單:無論如何在分組之前對行進行排序而不訴諸子查詢?

                  編輯:這個問題是另一個問題的延續,我的具體情況略有不同.您可以(并且應該)假設還有一個 wp_posts.id 是該特定帖子的唯一標識符.

                  Edit: This question was a continuation from another question and the specifics of my situation are slightly different. You can (and should) assume that there is also a wp_posts.id that is a unique identifier for that particular post.

                  推薦答案

                  在子查詢中使用 ORDER BY 并不是解決此問題的最佳方法.

                  Using an ORDER BY in a subquery is not the best solution to this problem.

                  按作者獲取 max(post_date) 的最佳解決方案是使用子查詢返回最大日期,然后將其加入到 post_author 上的表中和最大日期.

                  The best solution to get the max(post_date) by author is to use a subquery to return the max date and then join that to your table on both the post_author and the max date.

                  解決方案應該是:

                  SELECT p1.* 
                  FROM wp_posts p1
                  INNER JOIN
                  (
                      SELECT max(post_date) MaxPostDate, post_author
                      FROM wp_posts
                      WHERE post_status='publish'
                         AND post_type='post'
                      GROUP BY post_author
                  ) p2
                    ON p1.post_author = p2.post_author
                    AND p1.post_date = p2.MaxPostDate
                  WHERE p1.post_status='publish'
                    AND p1.post_type='post'
                  order by p1.post_date desc
                  

                  如果您有以下示例數據:

                  If you have the following sample data:

                  CREATE TABLE wp_posts
                      (`id` int, `title` varchar(6), `post_date` datetime, `post_author` varchar(3))
                  ;
                  
                  INSERT INTO wp_posts
                      (`id`, `title`, `post_date`, `post_author`)
                  VALUES
                      (1, 'Title1', '2013-01-01 00:00:00', 'Jim'),
                      (2, 'Title2', '2013-02-01 00:00:00', 'Jim')
                  ;
                  

                  子查詢將返回最大日期和作者:

                  The subquery is going to return the max date and author of:

                  MaxPostDate | Author
                  2/1/2013    | Jim
                  

                  然后,由于您將其加入到表格中,因此您將在這兩個值上返回該帖子的完整詳細信息.

                  Then since you are joining that back to the table, on both values you will return the full details of that post.

                  參見SQL Fiddle with Demo.

                  擴展我關于使用子查詢準確返回此數據的評論.

                  To expand on my comments about using a subquery to accurate return this data.

                  MySQL 不會強制您GROUP BY 包含在SELECT 列表中的每一列.因此,如果您只GROUP BY 一列但總共返回10 列,則無法保證返回屬于post_author 的其他列值.如果該列不在 GROUP BY 中,MySQL 將選擇應返回的值.

                  MySQL does not force you to GROUP BY every column that you include in the SELECT list. As a result, if you only GROUP BY one column but return 10 columns in total, there is no guarantee that the other column values which belong to the post_author that is returned. If the column is not in a GROUP BY MySQL chooses what value should be returned.

                  使用帶有聚合函數的子查詢將保證每次返回正確的作者和帖子.

                  Using the subquery with the aggregate function will guarantee that the correct author and post is returned every time.

                  作為旁注,雖然 MySQL 允許您在子查詢中使用 ORDER BY 并允許您將 GROUP BY 應用于不是 中的每一列>SELECT 列出此行為在包括 SQL Server 在內的其他數據庫中是不允許的.

                  As a side note, while MySQL allows you to use an ORDER BY in a subquery and allows you to apply a GROUP BY to not every column in the SELECT list this behavior is not allowed in other databases including SQL Server.

                  這篇關于MySQL order by before group by的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  How to use windowing functions efficiently to decide next N number of rows based on N number of previous values(如何有效地使用窗口函數根據 N 個先前值來決定接下來的 N 個行)
                  reuse the result of a select expression in the quot;GROUP BYquot; clause?(在“GROUP BY中重用選擇表達式的結果;條款?)
                  Does ignore option of Pyspark DataFrameWriter jdbc function ignore entire transaction or just offending rows?(Pyspark DataFrameWriter jdbc 函數的 ignore 選項是忽略整個事務還是只是有問題的行?) - IT屋-程序員軟件開發技
                  Error while using INSERT INTO table ON DUPLICATE KEY, using a for loop array(使用 INSERT INTO table ON DUPLICATE KEY 時出錯,使用 for 循環數組)
                  pyspark mysql jdbc load An error occurred while calling o23.load No suitable driver(pyspark mysql jdbc load 調用 o23.load 時發生錯誤 沒有合適的驅動程序)
                  How to integrate Apache Spark with MySQL for reading database tables as a spark dataframe?(如何將 Apache Spark 與 MySQL 集成以將數據庫表作為 Spark 數據幀讀取?)
                  • <legend id='NVXeH'><style id='NVXeH'><dir id='NVXeH'><q id='NVXeH'></q></dir></style></legend>
                      <tfoot id='NVXeH'></tfoot>
                      <i id='NVXeH'><tr id='NVXeH'><dt id='NVXeH'><q id='NVXeH'><span id='NVXeH'><b id='NVXeH'><form id='NVXeH'><ins id='NVXeH'></ins><ul id='NVXeH'></ul><sub id='NVXeH'></sub></form><legend id='NVXeH'></legend><bdo id='NVXeH'><pre id='NVXeH'><center id='NVXeH'></center></pre></bdo></b><th id='NVXeH'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='NVXeH'><tfoot id='NVXeH'></tfoot><dl id='NVXeH'><fieldset id='NVXeH'></fieldset></dl></div>

                      1. <small id='NVXeH'></small><noframes id='NVXeH'>

                          <tbody id='NVXeH'></tbody>
                        • <bdo id='NVXeH'></bdo><ul id='NVXeH'></ul>

                            主站蜘蛛池模板: 黄色在线免费观看 | 欧美一区二区在线观看 | 精品日韩 | 性高湖久久久久久久久 | 日韩中字幕 | 三级黄视频在线观看 | 日韩欧美精品 | 国产精品黄视频 | 成年人视频在线免费观看 | 日韩欧美专区 | 91精品一区二区三区久久久久 | 久久国产精品久久久久久 | 日韩欧美在线观看 | 欧美日韩成人网 | 日韩欧美在线视频观看 | 97精品超碰一区二区三区 | 日韩在线精品视频 | 国产精品一区在线观看 | 日本高清中文字幕 | 91精品国产91 | 中文成人在线 | 国产高清视频在线观看播放 | www.国产| 国产精久久久久久久妇剪断 | 午夜小电影 | 国产精品爱久久久久久久 | 亚洲精品成人在线 | 999精彩视频 | 久久久久久久久久爱 | 久久在线 | 99这里只有精品视频 | 精品久久久久久久 | 日韩看片| 人人艹人人爽 | 成人国产精品色哟哟 | 日本不卡一区二区三区 | 国产中文字幕亚洲 | 欧美日韩精品一区二区三区视频 | 日韩在线免费视频 | 奇色影视| 91xxx在线观看 |