WHERE语句,读取分隔字段

时间:2016-12-05 21:50:47

标签: sql sql-server tsql

我有以下查询以逗号分隔格式返回categories列。

SELECT * 
FROM (
    SELECT 
        l.LID, 
        Company, 
        Doors, 
        City, 
        Region, 
        Country, 
        Categories = STUFF((
            SELECT 
                CONVERT(varchar(100), Junc_CatID) + ', '
            FROM BND_ListingJunction_testing j
            WHERE j.Junc_LID = l.LID
            FOR XML PATH('')), 1, 2, '')
    FROM BND_Listing_testing l
    GROUP BY
        LID, Company, Doors, City, Region, Country
) x 
WHERE
    x.Categories = CONVERT(VARCHAR(50),2)

如何让我的where语句查看我的categories列,并确定它是否应该显示。意思是不考虑我的categories列中的1个长字符串,而是单个实体。

如果我的数据集是:

LID  Company  Doors   City  Region  Country Categories
1    a        a       a     AL      US      1,6,8,
2    b        b       b     AZ      US      2,6,
3    c        c       c     IA      US      8,40,28,30,

我的WHERE子句正在查看类别8,然后我的结果将是

LID  Company  Doors   City  Region  Country Categories
1    a        a       a     AL      US      1,6,8,
3    c        c       c     IA      US      8,40,28,30,

如果是6,那么我的结果将是

LID  Company  Doors   City  Region  Country Categories
1    a        a       a     AL      US      1,6,8,
2    b        b       b     AZ      US      2,6,

等...

5 个答案:

答案 0 :(得分:2)

您忘记发布部分查询。您需要的WHERE子句将添加到您忘记的部分。

在WHERE子句中,您将通过检查类别表上的子查询来确定是否应显示该行,如下所示(请查看底部的2行):

SELECT * 
FROM (
    SELECT 
        l.LID, 
        Company, 
        Doors, 
        City, 
        Region, 
        Country, 
        Categories = STUFF((
            SELECT 
                CONVERT(varchar(100), Junc_CatID) + ', '
            FROM BND_ListingJunction_testing j
            WHERE j.Junc_LID = l.LID
            FOR XML PATH('')), 1, 2, '')
            FROM BND_Listing_testing l
            GROUP BY LID, Company, Doors, City, Region, Country
        FROM WhateverThisTableIsCalled l
        WHERE LID IN (SELECT LID from BND_ListingJunction_testing WHERE Junc_CATID = @TheIdOfInterest)
) x 

答案 1 :(得分:2)

只需加入桌面即可测试是否存在

这将找到所有6。

SELECT * 
FROM (
    SELECT 
        l.LID, 
        Company, 
        Doors, 
        City, 
        Region, 
        Country, 
        Categories = STUFF((
            SELECT 
                CONVERT(varchar(100), Junc_CatID) + ', '
            FROM BND_ListingJunction_testing j
            WHERE j.Junc_LID = l.LID
            FOR XML PATH('')), 1, 2, '')
    FROM BND_Listing_testing l
    GROUP BY
        LID, Company, Doors, City, Region, Country
) x
JOIN BND_ListingJunction_testing j ON (j.Junc_LID, Junc_CatID) = (x.LID, 6)

或者更容易理解

SELECT * 
FROM (
    SELECT 
        l.LID, 
        Company, 
        Doors, 
        City, 
        Region, 
        Country, 
        Categories = STUFF((
            SELECT 
                CONVERT(varchar(100), Junc_CatID) + ', '
            FROM BND_ListingJunction_testing j
            WHERE j.Junc_LID = l.LID
            FOR XML PATH('')), 1, 2, '')
    FROM BND_Listing_testing l
    GROUP BY
        LID, Company, Doors, City, Region, Country
) X
WHERE EXISTS
   (SELECT 1 FROM BND_ListingJunction_testing j
    WHERE j.Junc_LID = x.LID and Junc_CatID = 6)

答案 2 :(得分:1)

在您的类别之前添加',','1,2,6,8',然后您可以轻松使用

where ',' + categories like '%,6,%'

答案 3 :(得分:1)

在你的比较中,你正在检查你可能永远不会得到的确切的平等。

x.Categories = CONVERT(VARCHAR(50),2)

-- becomes this when evaluated
x.Categories = '2'

相反,你想要的东西就像你的类别,或类似的东西

x.Categories LIKE '%,' + CONVERT(VARCHAR(50), 2) + ',%'

在您的类别列表前加上Tony的额外逗号。额外的逗号是为了防止'6,''2,26,86,'

匹配

答案 4 :(得分:1)

如果您使用的是compat等级130,string_split将起作用:

where '6' in (select value from string_split(Categories, ','))

或者,如果您可以使用前导,和尾随,构建数据,则可以使用

where Categories like `%,6,%`

然而,csv通常不像这样查询。规范化查询会更好。