2012-11-27 76 views
0

如果在使用group by选择时应用限制,限制将应用于返回的组数或行数?我想限制返回的组。MySQL“group by”限制组返回

这里是我的审判:

select * from orders group by customer_email limit 0,2 

从这个表:

id customer_email 
0 [email protected] 
1 [email protected] 
2 [email protected] 
3 [email protected] 
4 [email protected] 
5 [email protected] 
6 [email protected] 

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

+0

上述查询的结果是什么? – Hemantwagh07

+0

它将返回有限的组而不是行。你想限制组,但你需要返回行0 1 2 3 4 5? – Justin

+0

这应该会产生一个错误,因为您没有使用任何聚合函数,但使用了 – polin

回答

0

您的问题有点含糊:建议的查询限制为2个组,并且您似乎希望前面3个组中包含的所有行等于customer_email

不管怎么说,这是我的,据我正确理解你的问题:)

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。