Now I do it through two requests, using a loop to find the number of attached articles for each tag.

define("SQL_QUERIES_TAGS_LIST", " SELECT * FROM `tags` ORDER BY `name` "); define("SQL_QUERIES_TAGS_COUNT", " SELECT count(*) as `count` FROM `n_t` WHERE `n_t`.`id_tag` = ? GROUP BY `id_tag` "); 

n_t is a table of links with numbers of tags and articles.

+ Should be returned and tags with the number 0, which have no related articles.

    1 answer 1

     SELECT tags.id, tags.name, (SELECT COUNT(n_t.id) FROM n_t WHERE n_t.id_tag = tags.id) as count FROM tags ORDER BY tags.name 
    • Thanks, your answer made you think, but it gives an error because of the syntax. Here is what I need and it is without errors, correct your answer and I will accept it as correct. The point was in the GROUP BY and ORDER BY order. SELECT tags . id as id , tags . name as name , count (*) as count FROM tags , n_t WHERE tags . id = n_t . id_tag GROUP BY n_t . id_tag ORDER BY tags . name - user307399 10:51
    • And another feature was discovered that tags are not returned without attached records, but should be returned with a zero. - user307399
    • @andreikorzhyts Updated answer - Nikita Umnov
    • Thank. I again corrected the answer for myself: SELECT tags . id , tags . name , (SELECT count (*) FROM n_t WHERE n_t . id_tag = tags . id ) as count FROM tags ORDER BY name You can correct your answer again to fit the question, if you want. And if it is not difficult, then you can explain a little nested choice, I can not understand how it affects, how is it possible to count the orphan tags? - user307399
    • one
      This is a subquery, i.e. second query inside the first. Unlike join, it does not require mandatory matching and can return an empty result. - Nikita Umnov