A
A
akazakou2013-10-03 09:49:29
MySQL
akazakou, 2013-10-03 09:49:29

You need to select the 100th entry for each user

Here is an interesting task for the local bright minds.

There is a MySQL table:

CREATE TABLE `post` (
  `post_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` int(11) unsigned NOT NULL,
  PRIMARY KEY (`post_id`),
  KEY `user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


The post_id field is auto-incrementing and unique. The user_id field is not unique. Each user with his user_id can appear in this table from one to infinity times. The number of records in the table tends to 2 million.

Question. If the user occurs in the table 100 or more times, then you need to find out the post_id of the hundredth record to which the user belongs.
Those. the resulting selection must contain the post_id of the hundredth record by user and his user_id

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Samuel_Leonardo, 2013-10-03
@akazakou

SELECT user_id,(SELECT ps2.post_id  from post ps2 WHERE ps2.user_id=p.user_id ORDER BY ps2.post_id LIMIT 100,1  ) as post_id FROM (
    SELECT DISTINCT user_id FROM post 
) p

G
GreatRash, 2013-10-03
@GreatRash

It?
stackoverflow.com/questions/858746/how-do-you-select-every-n-th-row-from-mysql

S
Sergey, 2013-10-03
Protko @Fesor

SELECT * FROM (
    SELECT p.post_id, p.user_id, COUNT(p.post_id) as n FROM posts p GROUP BY p.user_id HEAVING n >= 100
) p
INNER JOIN users u
ON u.id = p.user_id

something along those lines...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question