gpt4 book ai didi

MySQL 查询零值

转载 作者:行者123 更新时间:2023-11-29 08:52:04 25 4
gpt4 key购买 nike

我有这两个表(文章和销售):

id  |  name     |
====+============
1 | milk |
2 | apple |
3 | bread |
... | ... |


id | idArticle | date |
====+==============+=============
1 | 2 | 2011-01-01 |
2 | 2 | 2011-01-01 |
3 | 3 | 2011-01-01 |
4 | 1 | 2011-01-02 |
... | ... | ... |

我需要获取 2011/01/01 的销售额:文章和计数

2011-01-01 Milk 0
2011-01-01 Apple 2
2011-01-01 Bread 1
...

但是我不知道如何显示“牛奶:0”,因为那天没有卖牛奶。

此查询不起作用:

SELECT s.date, a.name, COUNT(*)
FROM article a
LEFT JOIN sale s ON a.id = s.idArticle
WHERE s.date = "2011-01-01"
GROUP BY s.date, a.name

最佳答案

SELECT "2011-01-01" AS date, a.name, IFNULL(s.total, 0)
FROM article a
LEFT JOIN (
SELECT idArticle, COUNT(*) AS total
FROM sale
WHERE date = "2011-01-01"
GROUP BY idArticle
) AS s ON a.id = s.idArticle

关于MySQL 查询零值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10894324/

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