Pull the row with the smallest number in the column

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

For example topic_id post_id
fifteen
2 5
3 8
4 8

This way it will capture lines 1 and 3.

I donโ€™t even know where to start, so any help would be greatly appreciated.

+4
source share
4 answers
select post_id, min(topic_id) from YourTable group by post_id 
+5
source

I think you can do something like

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

Take a look at GROUP BY

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

select topic_id, min (post_id) from which group_ topic_id

0
source

Source: https://habr.com/ru/post/1336373/


All Articles