我正在使用mysql。
我有一个名为zim_new_usa的表
我想要一份所有类型为aol和gmail的电子邮件列表
该表有一个名为office state
美国有50个州,我希望每个州有500封电子邮件。我不知道如何在查询中编写组函数。 我试图使用的查询是
select * from zim_new_usa where
(email like '%@aol%' or email like '%@gmail%') and length(`office state`) = 2
答案 0 :(得分:1)
答案 1 :(得分:0)
无法调试, 但试试这个:
SET @num := 0, @state:= '';
SELECT t.email, t.`office state`,
@num := if(@state = t.`office state`, @num + 1, 1) AS row_number,
@state := t.`office state` AS dummy
FROM (
SELECT email, `office state`
FROM zim_new_usa
WHERE
(email LIKE '%@aol%' OR email LIKE '%@gmail%')
AND length(`office state`) = 2
ORDER BY `office state`
) as t
GROUP BY t.`office state`,t.email
HAVING row_number <= 5;
顺便说一句,尽量避免在标题表或列时使用空格。