1

如果我在使用 group by 选择时应用了限制,则该限制适用于返回的组数或行数?

我自己,我想限制返回的组。

这是我的试验:

select * from orders group by customer_email limit 0,2

从此表:

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

我想要返回行 0,1,2,3,4,5。

4

1 回答 1

0

您的问题有点模棱两可:建议的查询限制为 2 个组,您似乎想要customer_email前 3 个组中包含的所有相等的行。

无论如何,就我正确理解您的问题而言,这是我的答案:)

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

子查询select customer_email from orders group by customer_email limit 0,3选择前 3 个组,然后选择order包含与customer_email前 3 个组相同的所有行,这将为您提供第 0、1、2、3、4、5 行。

于 2012-11-27T08:00:03.463 回答