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?
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.
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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.