In the sql database, how do you ensure that all the data in the condition is checked out, and if it doesn't exist, replace it with 0?

data table test has the following data

{name: "a", parent_key: 1,value: 11},
{name: "b", parent_key: 3,value: 12},
{name: "c", parent_key: 3,value: 13},
{name: "d", parent_key: 3,value: 14},
{name: "e", parent_key: 2,value: 15},
{name: "f", parent_key: 1,value: 16}

use the SQL statement SELECT COUNT (name) FROM test WHERE parent_key in (1) GROUP BY parent_key; 2 3) GROUP BY parent_key; 4 5

in this way, values with parent_key of 4 and 5 are automatically ignored.
now I want to count all the count, of parent_key. If not, return 0, that is,

.
{parent_key: 1, count: 2},
{parent_key: 2, count: 1},
{parent_key: 3, count: 3},
{parent_key: 4, count: 0},
{parent_key: 5, count: 0},

in addition to finding the results and then assembling the results, can you directly use SQL statements to achieve it?

clipboard.png

clipboard.png

clipboard.png

Sql
Mar.12,2021

I don't know what you are talking about. No, there is no record of parent_key in your data table, so I will not count it.

SELECT `parent_key`,sum(`parent_key`) FROM `test` GROUP BY `parent_key`;
Menu