Jeg holdt fast i den lignende situation og fandt ud af, at MySql ikke giver nogen foruddefineret funktion til at overvinde dette problem.
For at overvinde oprettede jeg en UDF. Se venligst nedenfor om definitionen og brugen.
DROP FUNCTION IF EXISTS `get_unique_items`;
DELIMITER //
CREATE FUNCTION `get_unique_items`(str varchar(1000)) RETURNS varchar(1000) CHARSET utf8
BEGIN
SET @String = str;
SET @Occurrences = LENGTH(@String) - LENGTH(REPLACE(@String, ',', ''));
SET @ret='';
myloop: WHILE (@Occurrences > 0)
DO
SET @myValue = SUBSTRING_INDEX(@String, ',', 1);
IF (TRIM(@myValue) != '') THEN
IF((LENGTH(@ret) - LENGTH(REPLACE(@ret, @myValue, '')))=0) THEN
SELECT CONCAT(@ret,@myValue,',') INTO @ret;
END if;
END IF;
SET @Occurrences = LENGTH(@String) - LENGTH(REPLACE(@String, ',', ''));
IF (@occurrences = 0) THEN
LEAVE myloop;
END IF;
SET @String = SUBSTRING(@String,LENGTH(SUBSTRING_INDEX(@String, ',', 1))+2);
END WHILE;
SET @ret=concat(substring(@ret,1,length(@ret)-1), '');
return @ret;
END //
DELIMITER ;
Eksempel på brug:
SELECT get_unique_items('2,2,2,22,2,3,3,3,34,34,,54,5,45,,65,6,5,,67,6,,34,34,2,3,23,2,32,,3,2,,323') AS 'Items';
Resultat:
2,22,3,34,54,45,65,67,23,32,323
Håber dette hjælper!