以逗号分隔的重复值组

时间:2017-08-26 10:22:47

标签: mysql sql

我有一个包含这些值的表

SITE_NAME | CATEGORY |
----------------------
SITE1 | CAR, TRAVEL
SITE2 | TRAVEL
SITE3 | SPORT, GAME
SITE4 | GAME
SITE5 | CAR
SITE6 | TRAVEL
SITE7 | GAME

我想让它重复聚合值,所以我使用它:

SELECT category, COUNT (*) FROM table_db group by category having count (*)> = 1

这适用于对同等类别进行分组'价值观,但对待' CAR,TRAVEL'作为' CAR'以外的其他值我希望它也被识别为重复值。

此代码显示:

CAR, TRAVEL
TRAVEL
SPORT, GAME
CAR
GAME

我希望它看起来像这样:

CAR
TRAVEL
SPORT
GAME

1 个答案:

答案 0 :(得分:0)

虽然我完全同意有关数据库设计的其他评论,但如果由于某种原因,您仍然坚持使用您的设计,那么您需要创建自己的分割功能。像这样:

CREATE FUNCTION public.fnsplit(
    IN stringlist character varying,
    IN delimit character varying)
  RETURNS TABLE(items character varying) AS
$BODY$
declare remainderlist character varying;
declare front character varying;
declare delimitpos integer;
begin
    drop table if exists tmptbl;
    create temp table tmptbl(items character varying);
    remainderlist := $1;
    delimitpos := strpos(remainderlist, $2);
    while delimitpos > 0 loop
        front := trim(both from(left(remainderlist, delimitpos -1)));
        remainderlist := substr(remainderlist, delimitpos + 1);
        if length(front) > 0 then
            insert into tmptbl values (front);
        end if;
        delimitpos := strpos(remainderlist, $2);
    end loop;
    --insert last value
    remainderlist := trim(both from remainderlist);
    if length(remainderlist) > 0 then
        insert into tmptbl values (remainderlist);
    end if;
    return query
        select * from tmptbl;
        return;
end;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100
  ROWS 1000;

然后您可以在您的选择中使用它:

SELECT category, COUNT (*) FROM
(SELECT fnsplit(category, ', ') as category FROM table_db) d
group by category having count(*) >= 1;

我不禁要强调,这应该是最后的手段!

修改

有人指出OP需要MySQL。这有点棘手,因为MySQL不允许函数返回表。所以你必须使用临时表。所以现在函数看起来像这样:

DELIMITER $$
CREATE PROCEDURE fnsplit(
    stringlist varchar(2000),
    delimit varchar(20)
) 
BEGIN

declare remainderlist varchar(2000);
declare front varchar(2000);
declare delimitpos integer;

    SET remainderlist = stringlist;
    SET delimitpos = position(delimit in remainderlist);
    while delimitpos > 0 do
        SET front = trim(both from(left(remainderlist, delimitpos -1)));
        SET remainderlist = substr(remainderlist, delimitpos + 1);
        if length(front) > 0 then
            insert into tblTmpSplit values (front);
        end if;
        SET delimitpos = position(delimit in remainderlist);
    end while;
    SET remainderlist = trim(both from remainderlist);
    if length(remainderlist) > 0 then
        insert into tblTmpSplit values (remainderlist);
    end if;

END$$
DELIMITER ;

您现在可以这样称呼它:

SET @allcategories = (SELECT GROUP_CONCAT(category separator ', ') FROM table_db);

drop table if exists tbltmpsplit;
create temporary table tbltmpsplit(items varchar(2000));

call fnsplit(@allcategories, ', ');

SELECT *, Count(*) FROM tbltmpsplit GROUP BY items having count(*) >= 1;

drop table if exists tbltmpsplit;

返回:

CAR 2
GAME    3
SPORT   1
TRAVEL  3