Selecting distinct 2 columns combination in mysql

前端 未结 6 1642
日久生厌
日久生厌 2020-12-03 03:13

I have a mysql table that looks like this:

1   value1    value2    3534
2   value1    value1    8456
3   value1    value2    3566
4   value1    value3    734         


        
6条回答
  •  庸人自扰
    2020-12-03 03:28

    Update 1

    Better you use this against above.

    SELECT id, col2, col3, col4
    FROM yourtable
    GROUP BY col2, col3;
    

    Demo

    The reason I am saying is because using CONCAT, I am not getting desired result in this case. First query is returning me 5 rows however CONCAT is returning me 4 rows which is INCORRECT.

    Hope you got my point.


    Assumed the columns in the table are (id, col2, col3, col4).

    SELECT DISTINCT(CONCAT(col2, col3)) as "dummy column", id, col2, col3, col4
    FROM yourtable
    GROUP BY CONCAT(col2, col3);
    

    OR

    SELECT id, col2, col3, MIN(col4)
    FROM yourtable
    GROUP BY col2, col3;
    

    live working example

提交回复
热议问题