gpt4 book ai didi

mysql - 如何选择表行值作为值总和的列

转载 作者:可可西里 更新时间:2023-11-01 07:34:15 25 4
gpt4 key购买 nike

我有下表:

CREATE TABLE products
(
date DATE,
productname VARCHAR(80),
quantity INT(5)
);

INSERT INTO `products`(`date`, `productname`, `quantity`) VALUES ('2016-12-16','toy',5);
INSERT INTO `products`(`date`, `productname`, `quantity`) VALUES ('2016-12-18','santa',8);
INSERT INTO `products`(`date`, `productname`, `quantity`) VALUES ('2016-12-23','tree',15);
INSERT INTO `products`(`date`, `productname`, `quantity`) VALUES ('2016-11-16','toy',5);
INSERT INTO `products`(`date`, `productname`, `quantity`) VALUES ('2016-11-19','santa',2);
INSERT INTO `products`(`date`, `productname`, `quantity`) VALUES ('2016-11-24','tree',5);
INSERT INTO `products`(`date`, `productname`, `quantity`) VALUES ('2016-10-01','toy',10);
INSERT INTO `products`(`date`, `productname`, `quantity`) VALUES ('2016-10-04','santa',20);
INSERT INTO `products`(`date`, `productname`, `quantity`) VALUES ('2016-10-09','tree',30);
INSERT INTO `products`(`date`, `productname`, `quantity`) VALUES ('2016-10-01','toy',40);
INSERT INTO `products`(`date`, `productname`, `quantity`) VALUES ('2016-10-04','santa',30);
INSERT INTO `products`(`date`, `productname`, `quantity`) VALUES ('2016-10-09','tree',20)

我想查看数量总和,每个月的日期为每行一个,产品名称为列,因此我创建了如下查询:

SELECT DATE_FORMAT(`date`, '%Y-%m') As Date, 
IF(`productname` = 'santa', SUM(`quantity`), 'none') As santa,
IF(`productname` = 'toy', SUM(`quantity`), 'none') As toy,
IF(`productname` = 'tree', SUM(`quantity`), 'none') As tree

FROM `products`

GROUP BY DATE_FORMAT(`date`, '%Y-%m'),`productname`

这给了我这样的东西:

+---------+-------+------+------+
| Date | santa | toy | tree |
+---------+-------+------+------+
| 2016-10 | 50 | none | none |
+---------+-------+------+------+
| 2016-10 | none | 50 | none |
+---------+-------+------+------+
| 2016-10 | none | none | 50 |
+---------+-------+------+------+
| 2016-11 | 2 | none | none |
+---------+-------+------+------+
| 2016-11 | none | 5 | none |
+---------+-------+------+------+
| 2016-11 | none | none | 5 |
+---------+-------+------+------+
| 2016-12 | 8 | none | none |
+---------+-------+------+------+
| 2016-12 | none | 5 | none |
+---------+-------+------+------+
| 2016-12 | none | none | 15 |
+---------+-------+------+------+

这几乎不错,但我希望它是这样的,所以特定月份只有一行:

+---------+-------+------+------+
| Date | santa | toy | tree |
+---------+-------+------+------+
| 2016-10 | 50 | 50 | 50 |
+---------+-------+------+------+
| 2016-11 | 2 | 5 | 5 |
+---------+-------+------+------+
| 2016-12 | 8 | 5 | 15 |
+---------+-------+------+------+

是否可以通过查询来实现?

最佳答案

应该这样做

SELECT DATE_FORMAT(`date`, '%Y-%m') As Date,
IFNULL(Sum(Case when `productname` = 'santa' then `quantity` end),0) As santa,
IFNULL(Sum(Case when `productname` = 'toy' then `quantity` end),0) As toy,
IFNULL(Sum(Case when `productname` = 'tree' then `quantity` end),0) As tree
FROM `products`
GROUP BY DATE_FORMAT(`date`, '%Y-%m');

关于mysql - 如何选择表行值作为值总和的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41312388/

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