user3412566
user3412566

Reputation: 23

MySQL with selecting distinct row

Hello there, I have a schema like this, table name feeds

Where msg_id is unique and its the primary key of the table

|   msg_id    |commented|
|        1        |       10        |
|        2        |       10        |
|        3        |       10        |
|        4        |       21        |

I want to build a query that would select the last two rows

The output should go like this

|   msg_id    |commented|
|        3        |       10        |
|        4        |       21        |

In short the query should return the rows with msg_id which have a distinct commented value

Upvotes: 1

Views: 130

Answers (3)

bhavesh
bhavesh

Reputation: 68

SELECT * FROM feeds GROUP BY commented

Upvotes: -1

Sougata Bose
Sougata Bose

Reputation: 31749

Try this -

select max(msg_id), commented from your_table group by commented

Upvotes: 0

juergen d
juergen d

Reputation: 204894

Group by the column ment to be unique and select the highest id for every group

select max(msg_id) as msg_id, commented
from your_table
group by commented

Upvotes: 2

Related Questions