Jump to content

newest record for each user


sniperscope

Recommended Posts

Hi

I have a database called news and there is records something like this

 

news_id | user id | news | news_date

1                3            aaa    2012/4/1

2                2            bbb    2012/4/2

3                5            ccc      2012/4/3

4                3            ddd    2012/4/4

5                3            eee    2012/4/5

6                4            ffff      2012/4/6

7                3            ggg    2012/4/7

8                5            hhh    2012/4/8

9                4            iii        2012/4/9

 

and i want to get only one record for each user

so result should be

 

1  4  iii      2012/4/9  lisa

2  5  hhh  2012/4/8  mike

3  3  ggg  2012/4/7  jason

4  2  bbb  2012/4/2  david

 

and this is my query

SELECT n.user_id, n.news, n.news_date, s.user_name, s.city

FROM news_event n

LEFT JOIN user_master s ON n.user_id = s.id

WHERE s.city LIKE '%sydney%'

AND s.is_activated = 'y'

GROUP BY n.user_id 

ORDER BY n.news_date DESC

 

I know solution is so simple but i lost my capable of thinking after 18 hours of non stop work.

 

Thanks for any help.

Link to comment
Share on other sites

Okay

I've changed my query something like this

 

SELECT n.shop_id, n.news, MAX( n.news_date )

FROM news n

WHERE n.area = 'sydney'

GROUP BY n.user_id

ORDER BY n.news DESC

 

This time it takes MAX date but news is oldest one

 

This is how db holds the record

news_id | user id | news | news_date

1                3            aaa    2012/4/1

2                3            ddd    2012/4/4

3                3            eee    2012/4/5

4                3            ggg    2012/4/7

 

but above query gives me this

 

news_id | user id | news | news_date

1                3            aaa    2012/4/7

 

it should give me

 

news_id | user id | news | news_date

1                3            ggg    2012/4/7

Link to comment
Share on other sites

Use subquery to find max date for each user then join with this to match users and dates

SELECT
    N.user_id
  , N.news
  , N.news_date
FROM news N
INNER JOIN
(
  SELECT user_id, MAX(news_date) as maxdate
  FROM news
  GROUP BY user_id
) as X

ON N.user_id = X.user_id AND N.news_date = X.maxdate
ORDER BY N.news_date DESC

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.