Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select most popular tags from database?

I have two tables linked together through the 3rd table

threads: id, name
tags: id, name
thread_tag_map: threads_id, tags_id

Its a many to many relationship. I want to select 30 tags that are most popular that is to say the first 30 tags with tags_id which occur the most in thread_tag_map.

like image 784
never_had_a_name Avatar asked Dec 18 '25 23:12

never_had_a_name


1 Answers

SELECT
  t.*
FROM
  tags t
JOIN
  thread_tag_map ttm ON t.id = ttm.tags_id
GROUP BY
  t.id
ORDER BY
  COUNT(t.id) DESC
LIMIT 30
like image 134
hsz Avatar answered Dec 20 '25 16:12

hsz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!