简体   繁体   English

MySQL“分组依据”限制组返回

[英]MySQL “group by” limit groups returned

If I apply a limit when selecting with group by, the limit is applied to the number of groups returned or the number of rows? 如果在使用group by进行选择时应用了限制,该限制将应用于返回的组数或行数?

Myself, I want to limit the groups returned. 我本人想限制返回的团体。

Here's my trial: 这是我的审判:

select * from orders group by customer_email limit 0,2

From this table: 从此表:

id customer_email
0 a@a.com
1 a@a.com
2 a@a.com
3 b@b.com
4 c@c.com
5 c@c.com
6 d@d.com

I want returned rows 0,1,2,3,4,5. 我想要返回行0、1、2、3、4、5。

Your question is a little ambiguous: the proposed query limits to 2 groups, and you seem to want all the rows with equal customer_email that are contained in the first 3 groups. 您的问题有点模棱两可:建议的查询限制为2个组,并且您似乎希望前3个组中包含具有相同customer_email所有行。

Anyway, here is my answer for as far as I correctly understand your question :) 无论如何,据我正确理解您的问题,这是我的回答:)

SELECT *
FROM orders
INNER JOIN (
    SELECT customer_email
    FROM orders
    GROUP BY customer_email
    LIMIT 0,3
) AS temp
ON orders.customer_email = temp.customer_email

The sub query select customer_email from orders group by customer_email limit 0,3 selects the first 3 groups, then you select all the rows in order that contain the same customer_email as in the first 3 groups which would give you row 0, 1, 2, 3, 4, 5. 子查询select customer_email from orders group by customer_email limit 0,3选择select customer_email from orders group by customer_email limit 0,3选择前3个组,然后选择order中包含与前3个组相同的customer_email所有行,这将为您提供行0、1、2 3 4 5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM