J
J
Jack Cooper2016-01-15 20:48:28
PHP
Jack Cooper, 2016-01-15 20:48:28

MySQL fetch, count number?

Hello.
Users have reviews, all of them have different ratings from 1 to 5 (bad and excellent, respectively).
Tell me how to economically calculate their number, you need to find out how many reviews with a rating of 1, 2, etc. For the selected user.
Is it possible to do this in one request? And How?

Thanks in advance c:

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
nozzy, 2016-01-15
@negmat

select
user_id,
otziv_id,
SUM(CASE WHEN ocenka = 1 THEN 1 ELSE 0 END) ocenka_1,
SUM(CASE WHEN ocenka = 2 THEN 1 ELSE 0 END) ocenka_2,
SUM(CASE WHEN ocenka = 3 THEN 1 ELSE 0 END) ocenka_3,
....
from otziv_table
group by user_id, otziv_id
having user_id = #####

I
Ivan, 2016-01-15
@LiguidCool

Actually count function. To combine requests, they can be made, for example, nested.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question