R
R
rusgayfer2018-12-03 07:03:15
SQL
rusgayfer, 2018-12-03 07:03:15

How to make a selection in the database by key?

There is a table, how to find a user for a day who has more rows in the table?

SELECT * FROM `cover_wid_comments` WHERE `id_group` = 150444045

For example, you need to find the user who has the most comments in the database for today id_group=150444045, the user id isfrom_id
5c04aaa30db8a624901354.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2018-12-03
@rusgayfer

SELECT `from_id`
  FROM `table`
  WHERE `date` >= NOW() - INTERVAL 1 DAY
    AND `id_group` = :group
  GROUP BY `from_id`
  ORDER BY COUNT(*) DESC
  LIMIT 1

A
Alexander, 2018-12-03
@NeiroNx

In MySQL dialect like this

SELECT `from_id`, COUNT(`from_id`) as 'posts'
FROM `cover_wid_comments`
WHERE DATE_ADD(`date`, INTERVAL 1 DAY) > NOW() AND `id_group`=150444045 
GROUP BY `from_id`
ORDER BY count(`from_id`) DESC
LIMIT 1

this is the top user when sorting by the number of occurrences (repetitions) of the field value

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question