K
K
KarambiG2021-08-06 23:52:51
MySQL
KarambiG, 2021-08-06 23:52:51

How to group an array with data?

I retrieved data from the database, and got an array with the following data:

tag    | likes 
котики | 32 
собачки| NULL
котики | 44
попугаи| NULL
собачки| NULL

Each line is a viewed post with a specific tag (cats, dogs or parrots). And the likes field indicates whether this post has been liked or not. If this field contains ANY number, then the like is set. If NULL - the like is NOT set.

So, help to group the data by tags. So that the first digit shows the total number of likes (not null) for the tag. And the second figure showed the total number of lines for the tag. That is, like this:
array(
  котики => array(2, 2),
  собачки => array(0, 2),
  попугаи=> array(0, 1)
)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Gordinskiy, 2021-08-07
@KarambiG

SELECT tag, SUM(likes), COUNT(*)
FROM table_name
GROUP BY tag

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question