gpt4 book ai didi

mysql - mysql中的联合和汇总

转载 作者:行者123 更新时间:2023-11-30 22:43:56 24 4
gpt4 key购买 nike

当我将 header 与使用 rollup 构建的实际数据合并以将它们写入 .csv 文件时,我找不到导致此错误的原因。

CREATE TABLE `products` (
`id` int(11) default NULL,
`item` varchar(100) default NULL,
`value` int(11) default NULL
) ENGINE=MyISAM ;

INSERT INTO `products` VALUES (1,'Mobiles', '1000'),(5,'Mobiles', '2000'),(8,'Mobiles', 4000),(18,'Books',100),(28,'Books', 200),(28,'Books',400);

当我尝试下面的查询时,

 SELECT * FROM (
(SELECT 'ITEM', 'SUM')
UNION
(select item, sum(value) from products group by item with rollup)
) data;

我收到这个错误

ERROR 1221 (HY000): Incorrect usage of CUBE/ROLLUP and ORDER BY

提前致谢。

最佳答案

select 'ITEM', 'SUM'
union
select item, sum(value) from products group by item with rollup
;

结果:

+---------+------+
| ITEM | SUM |
+---------+------+
| ITEM | SUM |
| Books | 700 |
| Mobiles | 7000 |
| NULL | 7700 |
+---------+------+

关于mysql - mysql中的联合和汇总,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30215986/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com