查询过滤表,其中条件以逗号分隔的字符串给出

时间:2010-11-09 18:50:30

标签: sql sql-server-2005

我需要一些帮助来创建查询。下面是样本表:

Segment   Name
1         Vincent
1         Jules
1         Lance
2         Vincent
3         Marsellus
4         Vincent
5         Marsellus

问题是过滤此表。要过滤的列是Segment列,要过滤的内容是作为字符串参数给出的,例如“1,2”,这意味着我想要段1和2中的名称,返回集合:

Segment   Name
1         Vincent
2         Vincent

如何做到这一点?将非常感谢帮助。谢谢!

2 个答案:

答案 0 :(得分:1)

分割字符串时,您将获得值为1和2的行。 然后,您获取匹配计数=行数的行。

DECLARE @string varchar(8000)
SET @string = '1,2'

DECLARE @Mytable TABLE (Segment int, name varchar(8000))
INSERT @Mytable VALUES (1, 'Vincent')
INSERT @Mytable VALUES (1, 'Jules')
INSERT @Mytable VALUES (1, 'Lance')
INSERT @Mytable VALUES (2, 'Vincent')
INSERT @Mytable VALUES (3, 'Marsellus')
INSERT @Mytable VALUES (4, 'Vincent')
INSERT @Mytable VALUES (5, 'Marsellus')

;WITH Split AS
(
    SELECT SplitValue, COUNT(*) OVER() AS NumSplitValues
    FROM dbo.FN_ListToTable(',', @string)
)
SELECT
    [Name]
FROM
    @Mytable T
    JOIN
    SPLIT S ON T.Segment = S.SplitValue
GROUP BY
    [Name], S.NumSplitValues
HAVING
    COUNT(*) = S.NumSplitValues

如果您需要Segment,请加入回来

;WITH Split AS
(
    SELECT SplitValue, COUNT(*) OVER() AS NumSplitValues
    FROM dbo.FN_ListToTable(',', @string)
), Matches AS
(
    SELECT
        [Name]
    FROM
        @Mytable T
        JOIN
        SPLIT S ON T.Segment = S.SplitValue
    GROUP BY
        [Name], S.NumSplitValues
    HAVING
        COUNT(*) = S.NumSplitValues
)
SELECT
    T.*
FROM
    Split S
    JOIN
    @Mytable T ON T.Segment = S.SplitValue
    JOIN
    Matches M ON T.[Name] = M.[Name]

分割功能依赖于数字表,我从谷歌

抓取它
CREATE FUNCTION dbo.FN_ListToTable (
     @SplitOn  char(1)      --REQUIRED, the character to split the @List string on
    ,@List     varchar(8000)--REQUIRED, the list to split apart
)
RETURNS TABLE
AS
RETURN 
(
    SELECT
        SplitValue
        FROM (SELECT
                  LTRIM(RTRIM(SUBSTRING(List2, number+1, CHARINDEX(@SplitOn, List2, number+1)-number - 1))) AS SplitValue
                  FROM (
                           SELECT @SplitOn + @List + @SplitOn AS List2
                       ) AS dt
                      INNER JOIN Number n ON n.Number < LEN(dt.List2)
                  WHERE SUBSTRING(List2, number, 1) = @SplitOn
             ) dt2
        WHERE SplitValue IS NOT NULL AND SplitValue <> ''
);
GO 

答案 1 :(得分:0)

我通过创建一个将字符串解析为表的函数来解决这个问题。具体来说,我在Web表单中有一系列项目,而不是从表单中单独加载这些项目,我选择接受所有项目作为单个逗号分隔字符串。

/*********************************************************
** Parse A Comma Delimited String Into A Table
** Description: When A Web Page Sends An Array Of Data To
** The Server, The Array Is Comma Delimited.
** This Routine Returns All Of The Data From A Comma
** Delimited String Into A Table.
*********************************************************/
CREATE FUNCTION dbo.ParseByComma ( @String VARCHAR(600) )
RETURNS @TblSubString TABLE
(
 VarSubString VARCHAR(10)
)
AS
BEGIN
    DECLARE @intPos INT,
            @SubStr VARCHAR(10)
    -- Remove All Spaces
    SET @String = REPLACE(@String, ' ','')
    -- Find The First Comma
    SET @IntPos = CHARINDEX(',', @String)
    -- Loop Until There Is Nothing Left Of @String
    WHILE @IntPos > 0
    BEGIN
        -- Extract The String
        SET @SubStr = SUBSTRING(@String, 0, @IntPos)
        -- Insert The String Into The Table
        INSERT INTO @TblSubString (VarSubString) VALUES (@SubStr)
        -- Remove The String & Comma Separator From The Original
        SET @String = SUBSTRING(@String, LEN(@SubStr) + 2, LEN(@String) - LEN(@SubStr) + 1)
        -- Get The New Index To The String
        SET @IntPos = CHARINDEX(',', @String)
    END
    -- Return The Last One
    INSERT INTO @TblSubString (VarSubString) VALUES (@String)
    RETURN
END