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

      如何選擇每個類別的最新四個項(xiàng)目?

      How to SELECT the newest four items per category?(如何選擇每個類別的最新四個項(xiàng)目?)
          <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. 本文介紹了如何選擇每個類別的最新四個項(xiàng)目?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                問題描述

                我有一個項(xiàng)目數(shù)據(jù)庫.每個項(xiàng)目都使用類別表中的類別 ID 進(jìn)行分類.我正在嘗試創(chuàng)建一個列出每個類別的頁面,并在每個類別下方顯示該類別中的 4 個最新項(xiàng)目.

                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
                

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

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

                SELECT id FROM category

                然后迭代該數(shù)據(jù)并查詢每個類別的數(shù)據(jù)庫以獲取最新項(xiàng)目:

                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 個查詢并獲取所有這些數(shù)據(jù).我有 33 個類別,所以我認(rèn)為這可能有助于減少對數(shù)據(jù)庫的調(diào)用次數(shù).

                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 個電話不是什么大問題,我應(yīng)該用簡單的方法來做.

                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;
                

                我假設(shè) item 表的主鍵是 item_id,并且它是一個單調(diào)遞增的偽鍵.也就是說,item_id 中較大的值對應(yīng)于 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.

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

                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.

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

                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 標(biāo)準(zhǔn)窗口函數(shù)的支持.現(xiàn)在我們可以像其他 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;
                

                這篇關(guān)于如何選擇每個類別的最新四個項(xiàng)目?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

                相關(guān)文檔推薦

                How to use windowing functions efficiently to decide next N number of rows based on N number of previous values(如何有效地使用窗口函數(shù)根據(jù) N 個先前值來決定接下來的 N 個行)
                reuse the result of a select expression in the quot;GROUP BYquot; clause?(在“GROUP BY中重用選擇表達(dá)式的結(jié)果;條款?)
                Does ignore option of Pyspark DataFrameWriter jdbc function ignore entire transaction or just offending rows?(Pyspark DataFrameWriter jdbc 函數(shù)的 ignore 選項(xiàng)是忽略整個事務(wù)還是只是有問題的行?) - IT屋-程序員軟件開發(fā)技
                Error while using INSERT INTO table ON DUPLICATE KEY, using a for loop array(使用 INSERT INTO table ON DUPLICATE KEY 時出錯,使用 for 循環(huán)數(shù)組)
                pyspark mysql jdbc load An error occurred while calling o23.load No suitable driver(pyspark mysql jdbc load 調(diào)用 o23.load 時發(fā)生錯誤 沒有合適的驅(qū)動程序)
                How to integrate Apache Spark with MySQL for reading database tables as a spark dataframe?(如何將 Apache Spark 與 MySQL 集成以將數(shù)據(jù)庫表作為 Spark 數(shù)據(jù)幀讀取?)

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

                          主站蜘蛛池模板: 欧美精品xxx | 欧美视频在线观看一区 | 亚洲伦理精品 | 亚洲成人精品一区 | 午夜黄色影院 | 午夜一级片| 欧美综合久久 | 夜夜骑夜夜 | 精品无人国产偷自产在线 | 欧美极品在线 | 欧美理伦 | 黄色福利视频 | 超碰成人福利 | 天天插天天爽 | 久久在线播放 | 亚洲国产精品久久 | 国产做受入口竹菊 | 黄色小视频在线观看 | 91精品免费视频 | 国产成人免费在线 | 亚洲精品成人 | 欧美性色网 | 青青草综合网 | 秋霞午夜鲁丝一区二区老狼 | 午夜影院免费观看 | 欧美精品三区 | 三级黄色网址 | 日韩色在线 | 成av人片在线观看www | 二区在线观看 | 国产精品毛片一区视频播 | 亚洲人天堂| 欧美性猛交xxxx免费看久久久 | 日本视频免费观看 | 久久久久久黄色 | 精品国产一区二区三区久久久蜜月 | 欧美人xxxx | 中文国产字幕 | 免费av网址在线观看 | 久久av一区二区三区亚洲 | 午夜丁香 |