I wanted to get badge list with issued times
.
This is brief info of my tables.
table Badge
:
+----+-----------+
| id | name |
+----+-----------+
| 1 | professor |
| 2 | campaign |
| 3 | test |
+----+-----------+
table issue_history
:
+----+-----------+--------+
| id | badge_id | ts |
+----+-----------+--------+
| 1 | 1 | 0908 |
| 2 | 1 | 0909 |
| 3 | 3 | 0909 |
+----+-----------+--------*
To get result I used LEFT JOIN
.
SELECT
b.id,
b.name,
COUNT(*) AS issued_times
FROM
badge b
LEFT JOIN
issue_history h
ON
b.id = h.badge_id
GROUP BY
b.id
I expected result like below
+----+-----------+--------------+
| id | name | issued_times |
+----+-----------+--------------+
| 1 | professor | 2 |
| 2 | campaign | 0 |
| 3 | test | 1 |
+----+-----------+--------------+
But I got wrong result
+----+-----------+--------------+
| id | name | issued_times |
+----+-----------+--------------+
| 1 | professor | 2 |
| 2 | campaign | 1 |
| 3 | test | 1 |
+----+-----------+--------------+
As you can see, the issues times of campaign
badge is 0
.
But the result shows its value as 1
.
How can I fix this issue?