在SQL中结合LIKE和IN

时间:2009-06-19 19:48:14

标签: sql sql-server-2008

而不是执行:

选择一个 来自b
喜欢'a%'的人    或者像'b%'一样    或者像'c%'

有没有办法执行像这种伪代码一样的功能?

选择一个 来自b
IN IN('a%','b%','c%')

6 个答案:

答案 0 :(得分:9)

可能对你的例子过于具体,但你可以像'[a-c]%'那样做。除此之外,我不知道任何类似LIKE的IN语法

答案 1 :(得分:5)

您可以将条件值转储到临时表中(不使用通配符)。然后做一个像:

的连接
SELECT b.a FROM b INNER JOIN #likevals on b.a LIKE #likevals.value + '%'

WHERE #likevals看起来像:

value
------
a
b
c

答案 2 :(得分:3)

您可以使用

SELECT a
FROM b
WHERE a >= 'A'
AND a < 'D'

这将确保您最好地使用索引。

我敢打赌,另一个答案会给你一个索引扫描或表扫描。

答案 3 :(得分:2)

刚发现这个。有很多方法可以解决它,但这里有一种灵活的方法可以用于报告解决方案。我碰巧在过去遇到了同样的问题需要解决,从不传递问题; o)。如果不解析,请提前道歉 - 从记忆中掏出并摆弄,但你应该明白这个想法

CREATE FUNCTION fn_PatList (@PatList VARCHAR(200))
RETURNS @SearchList TABLE (Pattern VARCHAR(100))
AS 
BEGIN
 WHILE @PatList LIKE '%,%'
 BEGIN
  SELECT @PatList = LTRIM(RTRIM(@PatList))
  INSERT INTO @SearchList
   SELECT RTRIM(LEFT(@PatList,PATINDEX('%,%',@PatList)-1))
  SELECT @PatList = SUBSTRING(@PatListmPATINDEX('%,%',@PatList)+1, 8000)
 END
 INSERT INTO @SearchList 
  SELECT LTRIM(RTRIM(@PatList))
 RETURN
END

这是一般的要点 - 对目前为止的任何错误道歉。很快就会做的。

现在..

SELECT Names.Person
FROM Names
INNER JOIN Fn_PatList ('Fred%,John%') PatList
ON Names.Person LIKE PatList.Pattern

您可能希望允许空格,即逗号和其他各种垃圾之间的空格。简而言之,您现在可以使用您的函数来满足模式列表。希望满足您的要求 - 非常容易使用。可能也想考虑索引,但你可以从那里拿起它。

答案 4 :(得分:0)

没有。 IN运算符需要实际值。你能得到的最接近的是:

Select a
from b 
where a in (
    select a 
    from b
    WHERE a LIKE 'a%'
    OR a LIKE 'b%'
    OR a LIKE 'c%')

除了仅仅重复您对此特定情况的努力,因此我不会使用上述代码。基本上,您需要将实际值传递到IN语句中。这里要说的是你可以在IN语句中使用select语句来获取你的值。

此外,我假设您不是在尝试选择文字值'a','b','c'等,而且这些值代表了一些您不在乎的字符串向我们展示。

答案 5 :(得分:-1)

我有同样的问题,我不得不为它写一个脚本。 (如果它仍在你的脑海中)

这是我最终使用的内容:

 DECLARE @User INT, @MaskCount INT
     SET @User = 1
     Select @MaskCount = COUNT(*) from BI.dbo.ALL_Access_Masks

     WHILE (@User <= @MaskCount)
        Begin

        DECLARE @ProntoUserID Nvarchar (12)
        Set @ProntoUserID = (select Distinct [USER_ID] from BI.dbo.ALL_Access_Masks where [ID] = @User)

        DECLARE @DB Nvarchar (3)
        Set @DB = (select Distinct DB_Code from BI.dbo.ALL_Access_Masks where [ID] = @User)


        DECLARE @TopFlag INT
        SET @TopFlag = 1

            Drop Table #UserMasks 
            Drop Table #UserMasks1 

            Create Table #UserMasks
            ([RN] [Int] NOT NULL,
            [DB] [Nvarchar] (3) NOT NULL,
            [Mask] [Nvarchar](10) NOT NULL)

            Create Table #UserMasks1
            ([DB] [Nvarchar] (3) NOT NULL,
            [Mask] [Nvarchar](10) NOT NULL)

            Insert Into #UserMasks1 

                select @DB ,PD_Mk_1  from BI.dbo.ALL_Access_Masks where [ID] = @User and PD_Mk_1 is not NULL
                Union All select @DB ,PD_Mk_2  from BI.dbo.ALL_Access_Masks where [ID] = @User and PD_Mk_2  is not null and DB_Code = @DB
                Union All select @DB ,PD_Mk_3  from BI.dbo.ALL_Access_Masks where [ID] = @User and PD_Mk_3  is not null and DB_Code = @DB
                Union All select @DB ,PD_Mk_4  from BI.dbo.ALL_Access_Masks where [ID] = @User and PD_Mk_4  is not null and DB_Code = @DB
                Union All select @DB ,PD_Mk_5  from BI.dbo.ALL_Access_Masks where [ID] = @User and PD_Mk_5  is not null and DB_Code = @DB
                Union All select @DB ,PD_Mk_6  from BI.dbo.ALL_Access_Masks where [ID] = @User and PD_Mk_6  is not null and DB_Code = @DB
                Union All select @DB ,PD_Mk_7  from BI.dbo.ALL_Access_Masks where [ID] = @User and PD_Mk_7  is not null and DB_Code = @DB
                Union All select @DB ,PD_Mk_8  from BI.dbo.ALL_Access_Masks where [ID] = @User and PD_Mk_8  is not null and DB_Code = @DB
                Union All select @DB ,PD_Mk_9  from BI.dbo.ALL_Access_Masks where [ID] = @User and PD_Mk_9  is not null and DB_Code = @DB
                Union All select @DB ,PD_Mk_10 from BI.dbo.ALL_Access_Masks where [ID] = @User and PD_Mk_10 is not null and DB_Code = @DB


            Insert Into #UserMasks

                Select ROW_NUMBER() OVER ( PARTITION BY @DB ORDER BY Mask DESC) AS RN,DB,Mask from #UserMasks1
                where DB = @DB


        WHILE (@TopFlag <=(select COUNT(*) from #UserMasks))

                        BEGIN

                            Insert Into BI.dbo.Masked_Users
                            select @DB,@ProntoUserID,'PD' as 'Mask_Type',Code
                            from BI.dbo.Mask_Lookup M
                            where code like (select Mask + '%' from #UserMasks Where RN = @TopFlag)
                            and [Database] = @DB and M.Mask = 'PD'

        SET @TopFlag = @TopFlag + 1

                    End

        SET @User = @User + 1

End

GO