SELECT
dt2.Email,
dt2.Domain
FROM
(
SELECT
@row_num := IF(@dmn <> dt1.Domain, 1, @row_num + 1) AS row_no,
@dmn := dt1.Domain AS Domain,
dt1.Email
FROM
(
SELECT
Email,
Domain
FROM Table1
ORDER BY Domain
) AS dt1
CROSS JOIN (SELECT @row_num := 0,
@dmn := '') AS user_init_vars
) AS dt2
WHERE dt2.row_no <= 2
ORDER BY dt2.Domain
1条答案
按热度按时间niwlg2el1#
在MySQL8.0.2及更高版本中,这个问题可以通过使用窗口函数以一种不太冗长的方式来解决。
对于您的MySQL5.1.73版本,我们可以使用会话变量进行模拟:
db小提琴演示
您可以按照本教程了解此方法的基本工作原理:http://www.mysqltutorial.org/mysql-row_number/