2012-11-27 79 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。