1

I have 2 tables which I want to query from.

The first table (lets call it users) has these columns: id and name.

The second table (lets call it transactions) has these columns: user_id, amount and timestamp.

How do I find out the latest timestamp in transactions for each user in the users table?

EDIT:

Thanks for all your answers so far! Really appreciate it. How about, if I want to select users with the latest timestamp greater than a certain value?

mushroom
  • 1,909
  • 3
  • 16
  • 33

4 Answers4

3
SELECT u.*,MAX(t.timestamp) latest_timestamp
FROM transactions t
LEFT JOIN users u ON t.user_id=u.id
GROUP BY t.user_id

Should be what you're looking for. This query should select the latest transaction timestamp for each user.

Sean Johnson
  • 5,567
  • 2
  • 17
  • 22
  • Except this won't work, since all of the columns in users are not specified in the GROUP BY clause. :( – Stuart Ainsworth Jun 25 '12 at 04:25
  • I'm assuming that each user has a unique user id. It may even be possible for different users to have the same name, so I didn't include that field in my group by. – Sean Johnson Jun 25 '12 at 04:27
  • Thanks, this worked. (: Any idea why when I add `WHILE latest_timestamp > xxxxxxxxx`, it doesn't return the expected rows? – mushroom Jun 25 '12 at 04:33
  • One other thing, my query won't include users who have no transactions. Did you need those users included in the result set? To answer your question, it's WHERE, not WHILE. `... WHERE latest_timestamp > xxxxx` – Sean Johnson Jun 25 '12 at 04:35
  • Nop, I don't need users who have no transactions. Oh yep, and I meant `WHERE` but it doesn't work that way too. – mushroom Jun 25 '12 at 04:37
  • 1
    Oh, actually since the filter relies on the aggregate function of the GROUP BY clause, you need to use HAVING rather than WHERE. `... HAVING latest_timestamp>xxxxxx` – Sean Johnson Jun 25 '12 at 04:39
  • @StuartAinsworth actually, in mysql, you can have columns that arent in group by or aggregate functions. http://dev.mysql.com/doc/refman/5.0/en/group-by-hidden-columns.html it just uses any value – Jarry Jun 25 '12 at 16:35
1
SELECT 
     id,
     name,
     max(timestamp) 
FROM users 
INNER JOIN transactions 
ON users.id = transactions.user_id
GROUP BY id,name

that should give you te greatest timestamp for every user in the users table

Jarry
  • 1,891
  • 3
  • 15
  • 27
0

Try this

SELECT Users.name,MAX(T.timestamp) As LastTimeStamp
FROM Users 
LEFT JOIN Transactions as T ON Users.user_id=T.user_id    
GROUP BY Users.name    
Sudhakar B
  • 1,465
  • 9
  • 16
0

This query will do more than what you asked, but this is just in case your next question would be "what if I also wanted to know the amount that belongs to the maximum timestamp?":

SELECT id, name, t.`timestamp`, amount
FROM (SELECT id, name, MAX(`timestamp`) AS `timestamp`
    FROM users u
    INNER JOIN transactions t ON t.user_id=u.id
    GROUP BY id, name
) AS maxt
INNER JOIN transactions t 
ON t.user_id = maxt.id AND t.`timestamp` = maxt.`timestamp`
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309