开发者

MySQL: Pick which row when values are the same

开发者 https://www.devze.com 2023-03-20 16:59 出处:网络
Ok so I\'m working on a larger SQL statement, but I\'ve come across an issue that I cant seem to find a fix for elsewhere. If I simplify what I currently have, take a table like:

Ok so I'm working on a larger SQL statement, but I've come across an issue that I cant seem to find a fix for elsewhere. If I simplify what I currently have, take a table like:

    col_1     |     col_2     |      col_3
     100      |    default    |      Hello
     500      |    default    |      World
     500      |    override   |开发者_开发知识库      Other

I want to select all rows that have unique values from col_1, but when I run into duplicates I need to use col_2 to determine which of the duplicates to use.

So if I selected col_3 values from above I would get 'Hello' and 'Other'.

I know DISTINCT wont get me what I want and I've tried some grouping and ordering methods, but because the grouping is calculated first, it doesnt seem to be the answer.


You could try with ORDER BY in a subquery and then GROUP BY :

SELECT * FROM 
       (SELECT * FROM table ORDER BY col_1 DESC) 
GROUP BY col_2
0

精彩评论

暂无评论...
验证码 换一张
取 消