0

私はこれらの2つのテーブル(記事と販売)を持っています:

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
4

2 に答える 2

2
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
于 2012-06-05T09:09:03.907 に答える
0
SELECT a.name, COUNT(s.id)
FROM article a
LEFT JOIN sale s ON a.id = s.idArticle AND s.date = '2011-01-01'
GROUP BY a.name
于 2012-06-05T09:06:26.197 に答える