Search code examples
mysqldistinctgroup-concatdistinct-values

how to sql distinct value inside a column that have multiple string delimeter by ','


enter image description here

hello everyone , i'm trying to generate new table using mysql that have difference value (which means eliminating duplicate) in the same column that have so much of string.

I already try group_concat(distinct column_name asc column_name separator ',') but it doesn't give the result i want ,

Important: I want different values inside a column

The picture no 1 is before sql and the number 2 is after group_concat sql ,it's still having duplicate there.

Thank you


Solution

  • You can do it but you should take PM77-1s advise and you should normalize your data.

    That said... you can do it the following way with a temporary-table:

    CREATE TEMPORARY TABLE temp (val CHAR(255));
    SET @S1 = CONCAT("INSERT INTO temp (val) VALUES ('",REPLACE((SELECT GROUP_CONCAT( DISTINCT hashtag_name) AS data FROM hashes), ",", "'),('"),"');");
    PREPARE stmt1 FROM @s1;
    EXECUTE stmt1;
    SELECT group_concat(DISTINCT(val)) as hashtag_name FROM temp;
    

    I've made a working Fiddle. (because SQLFiddle is not workable these days I used rextester.com) http://rextester.com/HMUC89223

    Result:

        hashtag_name
    1   tido,makan,ittify,general,ittified,travel,lifestyle,minum,air,keyboard,lepak,gg,nice