E
E
Egor Sh.2016-04-13 17:51:52
MySQL
Egor Sh., 2016-04-13 17:51:52

How to select the most frequently mentioned tags from a database using SQL?

Good afternoon!
I have a table in the Tags database that stores all tags (they are unique, that is, there are no two records in this table with the same tag)
There is a Posts table
There is a Posts_Tags table in which tags and posts are linked,
for example, there is a tag # car with id= 5
and in Post_Tags this tag is associated with posts with id=7 id=10 and so on
like this
posts tags
7 ---------5
10--------5
20----- ---5
I want to select all posts in the last hour where some tags were mentioned more than others.
Theoretically, I can select all the tags for the last hour from the database and then sort them in the code, but maybe there is an easier way? Can you please tell me how can I implement this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexei, 2016-04-13
. @EgorkZe

select 
top 100
tags,
COUNTposts  
from 
(
select  tags, COUNT( posts ) as COUNTposts 
from  Posts_Tags
where  (указываешь диапазон времени)
group by tags
) x
order by COUNTposts

Either you remove the top 100 and write a condition for the selection.
It is possible certainly through HAVING to simplify still. But I don't know if it's available for MySQL and Postgresql...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question