dbTalk Databases Forums  

limit inside group in one query

comp.databases.mysql comp.databases.mysql


Discuss limit inside group in one query in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
friglob
 
Posts: n/a

Default limit inside group in one query - 08-18-2011 , 08:30 AM






i have this table architecture:

table user_media
user_id | media_id | date_media_added_ts

what i need to do is to select 10 newest media_id's for every user in
one query. there are only 10 users so no need to watch on the limit
there. it is maximum of 100 media_id's.
am i missing something very basic here?

thank you very much

Reply With Quote
  #2  
Old   
friglob
 
Posts: n/a

Default Re: limit inside group in one query - 08-18-2011 , 08:39 AM






On 8/18/11 3:30 PM, friglob wrote:
Quote:
i have this table architecture:

table user_media user_id | media_id | date_media_added_ts

what i need to do is to select 10 newest media_id's for every user in
one query. there are only 10 users so no need to watch on the limit
there. it is maximum of 100 media_id's. am i missing something very
basic here?

thank you very much


found the answer when googled the right way:

SELECT
media_id,user_id
FROM user_media
WHERE
(
SELECT count(*)
FROM user_media as R
WHERE R.user_id = user_media.user_id AND R.media_id > user_media.media_id
) <= 10
ORDER BY user_id ASC, date_added_ts DESC

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.