0

I want to use a query to get this result:

Postid(20) ---> 4 type(0) et 2 type(1)
Postid(21) ---> 3 type(0) et 3 type(1).

From this table:

id | userid | postid | type
1  |   465  |  20    |  0
2  |   465  |  21    |  1
3  |   466  |  20    |  1
4  |   466  |  21    |  0
5  |   467  |  20    |  0
6  |   467  |  21    |  0
7  |   468  |  20    |  1
8  |   468  |  21    |  1
9  |   469  |  20    |  0
10 |   469  |  21    |  1
11 |   470  |  20    |  0
12 |   470  |  21    |  0

I think I have to use GROUP BY, I tried it but I get no results.

How can I achieve that result?

Spoody
  • 2,852
  • 1
  • 26
  • 36
drubecepha
  • 11
  • 1

1 Answers1

0

You need to use an aggregation function, alongside the columns you want to group by in the SELECT part. Note: Any column that is selected alongside an aggregation function MUST come up in the GROUP BY section.

The following code should answer your question:

SELECT COUNT(id), postid, type FROM table_name GROUP BY postid, type

When using multiple GROUP BY columns, those entries that have all those columns in common will be grouped up, see here: https://stackoverflow.com/a/2421441/9743294

Matthias Bö
  • 449
  • 3
  • 12