1

I get the ids of a query by GROUP_CONCAT.

SELECT GROUP_CONCAT(ArticleID) FROM TagMap WHERE Tag IN(...)

How can I merge this query with a COUNT(*) group by

SELECT ArticleID,COUNT(*) FROM TagMap WHERE Tag IN(...) GROUP BY ArticleID

to get GROUP_CONCAT(ArticleID) with order of COUNT(*)?

In other words, in the first query, I want to make GROUP_CONCAT(ArticleID) order by the number of tags found for each ArticleID.

1

This would helpful, Give a try once

SELECT GROUP_CONCAT(ArticleID)
   ,COUNT(1) AS CNT
FROM (SELECT Tag
         ,ArticleID
         ,COUNT(1) AS CNT
  FROM TagMap Tag IN (...)
  GROUP BY Tag
           ,ArticleID) tmp
  GROUP BY Tag
  ORDER BY cnt
| improve this answer | |
1

The query can be next:

SELECT 
    -- here we concatenate articles by tags count 
    GROUP_CONCAT(ArticleID ORDER BY cnt DESC) 
FROM (
  -- here we calculate different tags cout per article
  SELECT ArticleID, COUNT(DISTINCT Tag) cnt
  FROM TagMap 
  WHERE Tag IN('Tag1', 'Tag2', 'Tag3')
  GROUP BY ArticleID
) t;

Working example here: PHPize.online

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.