Find lines of the last 24 hours that repeat the most (Mysql)

Asked

Viewed 322 times

0

'SELECT `from`, `to`, count(*) AS num_clicks
FROM my_rank
WHERE my_rank_data
BETWEEN "'.$yesterday.'" AND "'.$today.'"
ORDER BY num_clicks DESC LIMIT 0,20';

Above I am trying to take the fields "from" and "to" of a table, but I only want the lines of the last 24 hours, and I need to return only the 20 lines that most repeat of these 24 hours. However the above result returns me lines that are not the ones that are most repeated in the given date range.

  • The lines that repeat the most are the ones that have the most num_clicks?

  • I’m not sure, I think I did it wrong this "Count(*) AS num_clicks". My intention was that it was this "num_clicks", and being, what do I do with it? I will only have the number of times it repeats, I need the line that repeats most. I would use an ORDER BY num_clicks DESC?

2 answers

1

You can do it yes, I’d do it that way:

SELECT `from`, `to`, COUNT(*) AS num_clicks
FROM my_rank
WHERE my_rank_data >= DATE_SUB(NOW(), INTERVAL 24 HOUR)
GROUP BY `from`, `to`
ORDER BY num_clicks DESC
LIMIT 20;
  • Oops, I just updated the question. This was it GROUP BY from, to that should be missing then. Would have how to explain a little about it?

  • I updated the answer. You tried to run this query?

  • I don’t have your test base to check if it’s actually working, but it’s in that sense that it might solve your problem.

  • Yes, now it seems to work. Another question, my date in the bank field is like "2017-06-29 12:41:58", do you have a problem? because the variables $Yesterday and $Today are just so "2017-06-29".

  • In the database this in the correct format, remember that the function NOW() in the MySQL returns the current time. As you always want the ranking of the last 24 hours, I believe it is not necessary to use a variable that comes from PHP.

  • But if you still want to compare the bank date with the date you have on PHP, will have to convert. For example, "SELECT * FROM my_rank WHERE DATE(my_rank_data) = '$today'";.

Show 1 more comment

1


You can do something like this:

SELECT `from`, `to`, COUNT(*) AS num_clicks
FROM my_rank
WHERE my_rank_data >= NOW() - INTERVAL 1 DAY
GROUP BY `from`, `to`
ORDER BY num_clicks DESC

To use an aggregate function such as COUNT(), you need to indicate how you want to group the records (in this case, to do this count), and this you do with GROUP BY. So it will group the records that contain the same value in the fields from and to and generate a field by counting this repetition of records.

When organizing by the counter in a few clicks, descending, you will first have the most repeating records.

I consulted this post here:

Find Records with a date field in the last 24 hours [Uplicate]

and this one:

Find Most Frequent value in SQL column

to write this reply.

  • All that was missing was "LIMIT 20;"

  • I got complicated here, actually this "from" and "to" is the first key of another table, has how I select the values of this other table from the "from" and "to" that recover and lists the 20 with the value of the field name of the other table?

  • I didn’t put LIMIT 20 because when I first read your question I didn’t have that detail specified! So it depends. You could redeem fields from this other table by connecting the two tables through a JOIN, but to put more fields being returned by SELECT, these fields would have to be in the GROUP BY clause as well, because, remember that the fields returned by this SELECT came from several lines, so, you can only bring these fields if they are all equal in the lines being grouped.

  • https://answall.com/questions/278016/buscar-campo-de-tabela-com-resultado-de-query-de-umatabela-diferente?noredirect=1&lq=1 created a new question for this question.

  • For example, if the first line returned brought num_clicks=5, means that there were 5 rows in the table that had the same value in both fields, from and to. If you add a third field to be returned, it also has to have the same value in those 5 lines, otherwise it would not return the value of that third field in that first line. I don’t know if it’s clear.

  • I think then I would not, I will have to solve in PHP and pull for another query, because in the case the from and to are different and would return different rows from the other table, so in all would be 40 lines not 20, it would be difficult to separate them, it is better to create another query.

  • I took a look at the other question, but I’m not sure I understood it very well. In other_table the fields from and to will be primary key, which means there will only be one line for the combination of the two?

  • https://answall.com/questions/278016/buscar-campo-de-tabela-com-resultado-de-query-de-uma-tabela-diffchecks again, I edited, is the code I already got what I want, but I wanted to do this only with the query.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.