mahle
mahle

Reputation: 231

Pull row with lowest number in a column

I have two columns called topic_id and post_id, and what I want to do is find every row where the topic_id is the lowest for each post ID.

For example
topic_id post_id
1 5
2 5
3 8
4 8

So it would grab row 1 and 3.

I am not even sure where to begin so any help would be greatly appreciated.

Upvotes: 4

Views: 304

Answers (4)

piyush
piyush

Reputation: 621

select topic_id, min(post_id) from where group by topic_id

Upvotes: 0

DVK
DVK

Reputation: 129393

SELECT MIN(topic_id), post_id
FROM   T
GROUP BY post_id

Upvotes: 0

Aaron Hathaway
Aaron Hathaway

Reputation: 4315

I think you'd be able to do something like

SELECT      MIN(`topic_id`), `post_id`
FROM        `my_table`
GROUP BY    `post_id`

Take a look GROUP BY

Upvotes: 0

Joe Stefanelli
Joe Stefanelli

Reputation: 135799

select post_id, min(topic_id)
    from YourTable
    group by post_id

Upvotes: 5

Related Questions