C
C
chopik252022-01-25 18:18:10
PHP
chopik25, 2022-01-25 18:18:10

How to implement sorting?

Hello, the question will seem a little silly to you, but as a beginner you will help me a lot.
And so here's the problem.
There is a table of categories, with a large number of subcategories.
I need to display those with the most subcategories first.
Ie output in descending order.

using ORDER BY how to implement?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MrColdCoffee, 2022-01-25
@chopik25

All categories with subcategories sort by the number of subcategories in the category in descending order

SELECT ALL COUNT(parent_id) OVER(PARTITION BY id) AS idc
    , title , key ,desc , parent_id,id
FROM test order by idc desc

Only categories (id) sort by the number of categories subcategories in descending order
SELECT DISTINCT COUNT(parent_id) OVER(PARTITION BY id) idc
, id
FROM test order by idc desc

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question