WITH ROLLUP和DISTINCT的使用不正确

时间:2016-09-10 09:50:48

标签: mysql database rollup

我尝试将sql文件导入MySql数据库,但我遇到了一个问题:

ERROR 1221 (HY000) at line 4598: Incorrect usage of WITH ROLLUP and DISTINCT

DROP PROCEDURE IF EXISTS `sp_employees_rollup`$$
CREATE PROCEDURE `sp_employees_rollup` ()
BEGIN
SELECT Distinct City ,Sum(Salary) Salary_By_City FROM employees
GROUP BY City WITH ROLLUP;

END $$

DELIMITER ;

1 个答案:

答案 0 :(得分:3)

您不需要在DISTINCT City查询中使用GROUP BY,因为结果集中的每条记录都已经不同了。请改用此查询:

SELECT City
       SUM(Salary) Salary_By_City
FROM employees
GROUP BY City WITH ROLLUP