mcspiral
mcspiral

Reputation: 157

MySQL Select based on max of multiple columns

Say I have this table called Record

| user_id | work.id | trans.id |
|   1     |   1     |    1     |
|   1     |   2     |    1     |
|   1     |   3     |    2     |
|   2     |   4     |    3     |
|   2     |   5     |    3     |
|   3     |   6     |    4     |
|   3     |   7     |    5     |

How can I get a result based on the maximum of work.id and trans.id grouped by user_id?

| user_id | work.id | trans.id |
|   1     |   3     |    2     |
|   2     |   5     |    3     |
|   3     |   7     |    5     |

Any help would be appreciated. :)

Upvotes: 0

Views: 25

Answers (1)

Oto Shavadze
Oto Shavadze

Reputation: 42753

You need basic grouping with max():

 Select userid , max(workid), max(transid)   from    Record group by userid;

Upvotes: 1

Related Questions