如何限制每组的行数?

发布于 2024-12-04 15:30:12 字数 119 浏览 3 评论 0原文

我正在使用mysql。我不关心返回多少组,但如果单个组有超过 4 个项目,我只想要前 4 个。如何编写每组仅返回 4 行的语句?作为临时修复,我只是将它们全部返回并在代码中将其过滤掉。它仍然相当快,尽管如果我知道语法会更容易

I am using mysql. I don't care how many groups get returned back but if a single group has more then 4 items i only want the first 4. How do i write a statement that only returns 4rows per group? As a temporary fix i am just returning them all and filtering it out in code. Its still pretty fast although it would be easier if i knew syntax

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

山有枢 2024-12-11 15:30:12

如果我正确理解您的问题,我相信以下答案应该大致满足您的需要(注意:由于未提供您的表结构,我已经包含了两个测试表及其关联的插入作为示例):

给定这些表和数据:

DROP TABLE IF EXISTS tCustomer;
CREATE TABLE tCustomer (
     customerId INT(11) UNSIGNED NOT NULL auto_increment,
     name       VARCHAR(8),
     PRIMARY KEY (customerId)
) AUTO_INCREMENT=1;

INSERT INTO tCustomer VALUES
(NULL, 'Alex'),
(NULL, 'Bob'),
(NULL, 'Carl')
;

DROP TABLE IF EXISTS tPurchases;
CREATE TABLE tPurchases (
    purchaseId   INT(11) UNSIGNED NOT NULL auto_increment,
    customerId   INT(11) UNSIGNED NOT NULL,
    amount       DECIMAL(9,2),
    purchaseDate DATETIME,
    PRIMARY KEY (purchaseId),
    CONSTRAINT fk_customer FOREIGN KEY (customerId) REFERENCES tCustomer (customerId)
) AUTO_INCREMENT=1;

INSERT INTO tPurchases VALUES
(NULL, 1, 1.00, '2011-01-01 08:00'),
(NULL, 1, 1.01, '2011-01-02 08:00'),
(NULL, 1, 1.02, '2011-01-03 08:00'),
(NULL, 1, 1.03, '2011-01-04 08:00'),
(NULL, 1, 1.04, '2011-01-05 08:00'),
(NULL, 1, 1.05, '2011-01-06 08:00'),
(NULL, 2, 1.01, '2011-01-01 08:00'),
(NULL, 2, 1.02, '2011-01-02 08:00'),
(NULL, 3, 1.01, '2011-01-02 08:00'),
(NULL, 3, 1.02, '2011-01-04 08:00'),
(NULL, 3, 1.03, '2011-01-08 08:00')
;

以下 SQL 将按客户选择购买数据,返回不超过 4 次最近的购买:

SELECT
    tC.customerId, tC.name, tP.purchaseDate, tP.amount, COUNT(tPNewer.customerId) newerCt
FROM
                    tCustomer  tC
    INNER JOIN      tPurchases tP      ON tC.customerId = tP.customerId
    LEFT OUTER JOIN tPurchases tPNewer ON tP.customerId = tPNewer.customerId AND tPNewer.purchaseId > tP.purchaseId
GROUP BY
    tC.customerId,
    tC.name,
     tP.purchaseDate,
     tP.amount
HAVING
    newerCt < 4 -- Ignore rows that have more than 3 newer records
ORDER BY
     tC.customerId,
     tP.purchaseDate desc
;

以下是此选择的结果输出(请注意,Alex 最旧的交易不存在):

+------------+------+---------------------+--------+---------+
| customerId | name | purchaseDate        | amount | newerCt |
+------------+------+---------------------+--------+---------+
|          1 | Alex | 2011-01-06 08:00:00 |   1.05 |       0 |
|          1 | Alex | 2011-01-05 08:00:00 |   1.04 |       1 |
|          1 | Alex | 2011-01-04 08:00:00 |   1.03 |       2 |
|          1 | Alex | 2011-01-03 08:00:00 |   1.02 |       3 |
|          2 | Bob  | 2011-01-02 08:00:00 |   1.02 |       0 |
|          2 | Bob  | 2011-01-01 08:00:00 |   1.01 |       1 |
|          3 | Carl | 2011-01-08 08:00:00 |   1.03 |       0 |
|          3 | Carl | 2011-01-04 08:00:00 |   1.02 |       1 |
|          3 | Carl | 2011-01-02 08:00:00 |   1.01 |       2 |
+------------+------+---------------------+--------+---------+

If I understand your question correctly, I believe the following answer should do approximately what you need (note: I've included two test tables and their associated inserts as an example since your table structure was not provided):

Given these tables and data:

DROP TABLE IF EXISTS tCustomer;
CREATE TABLE tCustomer (
     customerId INT(11) UNSIGNED NOT NULL auto_increment,
     name       VARCHAR(8),
     PRIMARY KEY (customerId)
) AUTO_INCREMENT=1;

INSERT INTO tCustomer VALUES
(NULL, 'Alex'),
(NULL, 'Bob'),
(NULL, 'Carl')
;

DROP TABLE IF EXISTS tPurchases;
CREATE TABLE tPurchases (
    purchaseId   INT(11) UNSIGNED NOT NULL auto_increment,
    customerId   INT(11) UNSIGNED NOT NULL,
    amount       DECIMAL(9,2),
    purchaseDate DATETIME,
    PRIMARY KEY (purchaseId),
    CONSTRAINT fk_customer FOREIGN KEY (customerId) REFERENCES tCustomer (customerId)
) AUTO_INCREMENT=1;

INSERT INTO tPurchases VALUES
(NULL, 1, 1.00, '2011-01-01 08:00'),
(NULL, 1, 1.01, '2011-01-02 08:00'),
(NULL, 1, 1.02, '2011-01-03 08:00'),
(NULL, 1, 1.03, '2011-01-04 08:00'),
(NULL, 1, 1.04, '2011-01-05 08:00'),
(NULL, 1, 1.05, '2011-01-06 08:00'),
(NULL, 2, 1.01, '2011-01-01 08:00'),
(NULL, 2, 1.02, '2011-01-02 08:00'),
(NULL, 3, 1.01, '2011-01-02 08:00'),
(NULL, 3, 1.02, '2011-01-04 08:00'),
(NULL, 3, 1.03, '2011-01-08 08:00')
;

The following SQL will select purchase data by customer returning no more than the 4 most recent purchases:

SELECT
    tC.customerId, tC.name, tP.purchaseDate, tP.amount, COUNT(tPNewer.customerId) newerCt
FROM
                    tCustomer  tC
    INNER JOIN      tPurchases tP      ON tC.customerId = tP.customerId
    LEFT OUTER JOIN tPurchases tPNewer ON tP.customerId = tPNewer.customerId AND tPNewer.purchaseId > tP.purchaseId
GROUP BY
    tC.customerId,
    tC.name,
     tP.purchaseDate,
     tP.amount
HAVING
    newerCt < 4 -- Ignore rows that have more than 3 newer records
ORDER BY
     tC.customerId,
     tP.purchaseDate desc
;

Here is the resulting output from this select (note that Alex's oldest transactions are absent):

+------------+------+---------------------+--------+---------+
| customerId | name | purchaseDate        | amount | newerCt |
+------------+------+---------------------+--------+---------+
|          1 | Alex | 2011-01-06 08:00:00 |   1.05 |       0 |
|          1 | Alex | 2011-01-05 08:00:00 |   1.04 |       1 |
|          1 | Alex | 2011-01-04 08:00:00 |   1.03 |       2 |
|          1 | Alex | 2011-01-03 08:00:00 |   1.02 |       3 |
|          2 | Bob  | 2011-01-02 08:00:00 |   1.02 |       0 |
|          2 | Bob  | 2011-01-01 08:00:00 |   1.01 |       1 |
|          3 | Carl | 2011-01-08 08:00:00 |   1.03 |       0 |
|          3 | Carl | 2011-01-04 08:00:00 |   1.02 |       1 |
|          3 | Carl | 2011-01-02 08:00:00 |   1.01 |       2 |
+------------+------+---------------------+--------+---------+
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文