How to remove duplicate comma separated value in a single column in MySQL

后端 未结 3 1892
终归单人心
终归单人心 2020-12-19 08:40

\"enter

SELECT id, country FROM my_records

I\'ve got the abo

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-19 09:14

    The other solutions given are case insensitive, If you want result to be case sensitive and NOT remove accented matches like 'o' and 'ö', and looking for exact and strict match, here is the code

    If utf8mb4_bin does not work, try utf8_bin or other binary types.

    DELIMITER //
    
    DROP FUNCTION IF EXISTS `find_duplicate_using_comma` //
    CREATE FUNCTION `find_duplicate_using_comma` (in_str LONGTEXT) RETURNS LONGTEXT
    DETERMINISTIC
    NO SQL
    BEGIN
    
    
    DECLARE out_str LONGTEXT DEFAULT NULL; -- pending output
    DECLARE next_str TEXT DEFAULT NULL;    -- next element under consideration
    
    dedup:
    LOOP
    
      IF CHAR_LENGTH(TRIM(in_str)) = 0 OR in_str IS NULL THEN
        LEAVE dedup; -- no more data to consider
      END IF;
    
      SET next_str = SUBSTRING_INDEX(in_str,',',1);                   -- find the next element
      SET in_str = SUBSTRING(in_str FROM (CHAR_LENGTH(next_str) + 1 + 1)); -- remove that element
    
      SET in_str = TRIM(in_str), next_str = TRIM(next_str); -- trim the new and the rest
    
      IF FIND_IN_SET(next_str collate utf8mb4_bin,out_str collate utf8mb4_bin) OR CHAR_LENGTH(next_str) = 0 THEN -- if empty or already found
        ITERATE dedup;
      END IF;
    
      SET out_str = CONCAT_WS(',',out_str,next_str); -- append the new to pending output 
    
    END LOOP;
    
    RETURN out_str;
    
    END //
    
    DELIMITER ;
    

提交回复
热议问题