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

      <bdo id='0wK2y'></bdo><ul id='0wK2y'></ul>
  1. <i id='0wK2y'><tr id='0wK2y'><dt id='0wK2y'><q id='0wK2y'><span id='0wK2y'><b id='0wK2y'><form id='0wK2y'><ins id='0wK2y'></ins><ul id='0wK2y'></ul><sub id='0wK2y'></sub></form><legend id='0wK2y'></legend><bdo id='0wK2y'><pre id='0wK2y'><center id='0wK2y'></center></pre></bdo></b><th id='0wK2y'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='0wK2y'><tfoot id='0wK2y'></tfoot><dl id='0wK2y'><fieldset id='0wK2y'></fieldset></dl></div>
    <legend id='0wK2y'><style id='0wK2y'><dir id='0wK2y'><q id='0wK2y'></q></dir></style></legend>
      <tfoot id='0wK2y'></tfoot>
    1. <small id='0wK2y'></small><noframes id='0wK2y'>

      如何選擇每個類別的最新四個項目?

      How to SELECT the newest four items per category?(如何選擇每個類別的最新四個項目?)
          <tbody id='X8kHp'></tbody>
          <legend id='X8kHp'><style id='X8kHp'><dir id='X8kHp'><q id='X8kHp'></q></dir></style></legend>

        1. <tfoot id='X8kHp'></tfoot>
            <bdo id='X8kHp'></bdo><ul id='X8kHp'></ul>

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

                <i id='X8kHp'><tr id='X8kHp'><dt id='X8kHp'><q id='X8kHp'><span id='X8kHp'><b id='X8kHp'><form id='X8kHp'><ins id='X8kHp'></ins><ul id='X8kHp'></ul><sub id='X8kHp'></sub></form><legend id='X8kHp'></legend><bdo id='X8kHp'><pre id='X8kHp'><center id='X8kHp'></center></pre></bdo></b><th id='X8kHp'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='X8kHp'><tfoot id='X8kHp'></tfoot><dl id='X8kHp'><fieldset id='X8kHp'></fieldset></dl></div>
              1. 本文介紹了如何選擇每個類別的最新四個項目?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                我有一個項目數據庫.每個項目都使用類別表中的類別 ID 進行分類.我正在嘗試創建一個列出每個類別的頁面,并在每個類別下方顯示該類別中的 4 個最新項目.

                I have a database of items. Each item is categorized with a category ID from a category table. I am trying to create a page that lists every category, and underneath each category I want to show the 4 newest items in that category.

                例如:

                寵物用品

                img1
                img2
                img3
                img4
                

                寵物食品

                img1
                img2
                img3
                img4
                

                我知道我可以通過像這樣查詢每個類別的數據庫來輕松解決這個問題:

                I know that I could easily solve this problem by querying the database for each category like so:

                SELECT id FROM category

                然后迭代該數據并查詢每個類別的數據庫以獲取最新項目:

                Then iterating over that data and querying the database for each category to grab the newest items:

                SELECT image FROM item where category_id = :category_id 
                ORDER BY date_listed DESC LIMIT 4

                我想弄清楚的是,我是否可以只使用 1 個查詢并獲取所有這些數據.我有 33 個類別,所以我認為這可能有助于減少對數據庫的調用次數.

                What I'm trying to figure out is if I can just use 1 query and grab all of that data. I have 33 categories so I thought perhaps it would help reduce the number of calls to the database.

                有誰知道這可能嗎?或者如果 33 個電話不是什么大問題,我應該用簡單的方法來做.

                Anyone know if this is possible? Or if 33 calls isn't that big a deal and I should just do it the easy way.

                推薦答案

                這是最大的 n-per-group 問題,也是一個非常常見的 SQL 問題.

                This is the greatest-n-per-group problem, and it's a very common SQL question.

                這是我使用外連接解決它的方法:

                Here's how I solve it with outer joins:

                SELECT i1.*
                FROM item i1
                LEFT OUTER JOIN item i2
                  ON (i1.category_id = i2.category_id AND i1.item_id < i2.item_id)
                GROUP BY i1.item_id
                HAVING COUNT(*) < 4
                ORDER BY category_id, date_listed;
                

                我假設 item 表的主鍵是 item_id,并且它是一個單調遞增的偽鍵.也就是說,item_id 中較大的值對應于 item 中較新的行.

                I'm assuming the primary key of the item table is item_id, and that it's a monotonically increasing pseudokey. That is, a greater value in item_id corresponds to a newer row in item.

                這是它的工作原理:對于每個項目,都有一些其他較新的項目.例如,有比第四個最新項目更新的三個項目.零個項目比最新項目新.因此,我們希望將每個項目 (i1) 與較新且與 i1 具有相同類別的項目集 (i2) 進行比較.如果這些新項目的數量少于四個,i1 就是我們包含的項目之一.否則,請不要包含它.

                Here's how it works: for each item, there are some number of other items that are newer. For example, there are three items newer than the fourth newest item. There are zero items newer than the very newest item. So we want to compare each item (i1) to the set of items (i2) that are newer and have the same category as i1. If the number of those newer items is less than four, i1 is one of those we include. Otherwise, don't include it.

                此解決方案的美妙之處在于,無論您擁有多少個類別,它都能正常工作,并且在您更改類別時繼續工作.即使某些類別中的項目數少于四個,它也能正常工作.

                The beauty of this solution is that it works no matter how many categories you have, and continues working if you change the categories. It also works even if the number of items in some categories is fewer than four.

                另一種可行但依賴于 MySQL 用戶變量功能的解決方案:

                Another solution that works but relies on the MySQL user-variables feature:

                SELECT *
                FROM (
                    SELECT i.*, @r := IF(@g = category_id, @r+1, 1) AS rownum, @g := category_id
                    FROM (@g:=null, @r:=0) AS _init
                    CROSS JOIN item i
                    ORDER BY i.category_id, i.date_listed
                ) AS t
                WHERE t.rownum <= 3;
                

                <小時>

                MySQL 8.0.3 引入了對 SQL 標準窗口函數的支持.現在我們可以像其他 RDBMS 一樣解決這類問題:


                MySQL 8.0.3 introduced support for SQL standard window functions. Now we can solve this sort of problem the way other RDBMS do:

                WITH numbered_item AS (
                  SELECT *, ROW_NUMBER() OVER (PARTITION BY category_id ORDER BY item_id) AS rownum
                  FROM item
                )
                SELECT * FROM numbered_item WHERE rownum <= 4;
                

                這篇關于如何選擇每個類別的最新四個項目?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 數據幀讀取?)

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

                          <small id='96g7E'></small><noframes id='96g7E'>

                          <tfoot id='96g7E'></tfoot>
                            <tbody id='96g7E'></tbody>

                          主站蜘蛛池模板: 欧美日韩国产一区二区 | 99久久久久| 国产成人精品一区二区 | 欧美精品一区三区 | www.黄色在线观看 | 欧美激情在线精品一区二区三区 | 色综合网站| 日韩三区在线观看 | 91视频在线观看免费 | 国产精品久久9 | 一区二区三区亚洲 | 欧美精品成人 | 中文字幕在线一 | 日韩成人中文字幕 | 欧美久久一区二区 | 国产精品视频免费观看 | 免费一区在线观看 | 亚洲不卡av在线 | 狠狠躁夜夜躁人人爽天天高潮 | 国产美女在线看 | 蜜臀网| 黄色香蕉视频在线观看 | 欧美日韩在线精品 | 久久久久久久久久久久久久av | 日韩欧美在线播放 | 奇米在线| 一区二区三区久久久 | 国产精品毛片一区二区在线看 | 国产精品一区在线观看 | 成人国产精品免费观看 | 欧美老少妇一级特黄一片 | 日本一区二区三区精品视频 | 国产精品一二区 | 国产成人精品免费 | 桃色五月| 国产精品久久久久久久白浊 | 国产探花在线精品一区二区 | 欧美精品一二三 | 男女爱爱网站 | 天天操夜夜操 | 国产电影一区二区在线观看 |