E
E
Eugene2019-08-02 10:29:42
Yii
Eugene, 2019-08-02 10:29:42

How to count how many of each id is in the database?

There is a database orders. In which, for example, the fields id caregory_id, etc.
I need to count how many orders for a certain category. That is, how many tempers by category_id = 1, category_id = 2, and so on and take the 5 largest.
How to conjure such a request?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Novikov, 2019-08-02
@evgen9586

COUNT() + GROUP BY
https://www.w3resource.com/sql/aggregate-functions...

E
Evgenya-k2, 2019-08-02
@Evgenya-k2

Yii::$app->db->createCommand('select caregory_id, count(*) from table_name group by caregory_id order by count(*) desc limit 5')->queryAll();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question