R
R
Roman Savitsky2018-05-04 14:54:10
Yii
Roman Savitsky, 2018-05-04 14:54:10

How to display categories that have entries?

Hello !
I have two tables in the database, List of Categories and Records. In the Records table, I store the id of the selected Category when publishing.
Faced such a task, I want to display a list on the page, which will contain the following information:
Category (if it has at least one entry) - and under it the last 5 entries.
And so one under the other.
Tell me how to organize something like this in Yii2.
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem, 2018-05-04
@PRC

SELECT c.category_id, COUNT(r.record_id) AS records
FROM category AS c
LEFT JOIN records AS r USING(record_id)
GROUP BY c.record_id
HAVING records > 5

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question