47

因为我认为这应该是一个基本问题,所以我知道可能有人问过这个问题,但我找不到它。我可能即将获得 Peer Pressure 徽章,但我还是会问:

SQL Server 中是否有一种我不知道在使用 IN 时使用通配符 % 的方法。

我意识到我可以使用 OR 之类的:

select *
from jobdetails
where job_no like '0711%' or job_no like '0712%'

在某些情况下,我可以使用如下子查询:

select *
from jobdetails
where job_no in (select job_no from jobs where job_id = 39)

但我想做类似以下的事情:

select *
from jobdetails
where job_no in ('0711%', '0712%')

在这种情况下,它使用百分号作为字符而不是通配符,因此不会返回任何行。当我必须这样做时,我目前只使用一堆 OR,但我知道必须有更好的方法。你用什么方法来做到这一点?

4

15 回答 15

29

怎么样:

WHERE LEFT(job_no, 4) IN ('0711', '0712', ...)
于 2009-07-02T19:05:15.320 回答
15

我想我有一个简单的形式来解决这个调查的发起人想要什么。它对我有用,实际上这就是我来到这里开始的原因。我相信只需在列周围使用括号,如 '%text%' 并结合ORs 就可以了。

select * from tableName
where (sameColumnName like '%findThis%' or sameColumnName like '%andThis%' or 
sameColumnName like '%thisToo%' or sameColumnName like '%andOneMore%') 
于 2010-11-30T17:01:59.283 回答
11

这样的事情怎么样?

declare @search table
(
    searchString varchar(10)
)

-- add whatever criteria you want...
insert into @search select '0711%' union select '0712%'

select j.*
from jobdetails j
    join @search s on j.job_no like s.searchString
于 2009-07-02T19:15:22.880 回答
8

你可以尝试这样的事情:

select *
from jobdetails
where job_no like '071[12]%'

不完全是您要问的,但它具有相同的效果,并且在其他方​​面也很灵活:)

于 2009-07-02T19:05:23.417 回答
4

I had a similar goal - and came to this solution:

select *
from jobdetails as JD
where not exists ( select code from table_of_codes as TC 
                      where JD.job_no like TC.code ) 

I'm assuming that your various codes ('0711%', '0712%', etc), including the %, are stored in a table, which I'm calling *table_of_codes*, with field code.

If the % is not stored in the table of codes, just concatenate the '%'. For example:

select *
from jobdetails as JD
where not exists ( select code from table_of_codes as TC 
                      where JD.job_no like concat(TC.code, '%') ) 

The concat() function may vary depending on the particular database, as far as I know.

I hope that it helps. I adapted it from:

http://us.generation-nt.com/answer/subquery-wildcards-help-199505721.html

于 2012-11-14T09:24:21.293 回答
4
SELECT c.* FROM(
SELECT '071235' AS token UNION ALL SELECT '07113' 
 UNION ALL SELECT '071343'
UNION ALL SELECT '0713SA'
UNION ALL SELECT '071443') AS c
JOIN (
SELECT '0712%' AS pattern UNION ALL SELECT '0711%' 
 UNION ALL SELECT '071343') AS d
ON c.token LIKE d.pattern

071235
07113
071343
于 2009-07-02T19:11:57.117 回答
2
  1. 我首先添加了一个静态表,其中包含我的通配符结果的所有可能性(这家公司有一个 4 个字符的 nvarchar 代码作为他们的地区,他们通配符他们的当地人),即他们可能有 456?这会给他们 456[1] 到 456[Z] 即 0-9 & az

  2. 我必须编写一个脚本来提取当前用户(声明它们)并为声明的用户提取掩码。

  3. 创建一些临时表只是基本表来为当前用户排名行号

  4. 循环遍历每个结果(你的或这个或那个等......)

  5. 插入测试表。

这是我使用的脚本:

Drop Table #UserMasks 
Drop Table #TESTUserMasks 

Create Table #TESTUserMasks (
    [User] [Int] NOT NULL,
    [Mask] [Nvarchar](10) NOT NULL)

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

DECLARE @User INT
SET @User = 74054

Insert Into #UserMasks 
select ROW_NUMBER() OVER ( PARTITION BY ProntoUserID ORDER BY Id DESC) AS RN,
       REPLACE(mask,'?','') Mask
from dbo.Access_Masks 
where prontouserid = @User

DECLARE @TopFlag INT
SET @TopFlag = 1

WHILE (@TopFlag <=(select COUNT(*) from #UserMasks))
BEGIN
    Insert Into #TestUserMasks 
    select (@User),Code from dbo.MaskArrayLookupTable 
    where code like (select Mask + '%' from #UserMasks Where RN = @TopFlag)

    SET @TopFlag = @TopFlag + 1
END
GO

select * from #TESTUserMasks
于 2011-11-21T20:37:04.993 回答
1

IN运算符只不过是对“=”比较的一种幻想OR。事实上,在 SQL 2000 中,当列表包含大约 10k 个条目时,由于INinto ORs 的扩展存在堆栈溢出错误(是的,有人写 10k IN 条目......)。所以你不能在其中使用任何通配符匹配。

于 2009-07-02T19:13:42.073 回答
1

正如杰里米史密斯发布的那样,我会回顾一下,因为我无法回答他的那个特定问题。

select *
from jobdetails
where job_no like '071[1-2]%'

如果你只是需要0711%0712%你也可以在括号内放置一个范围。对于NOT关键字,您也可以使用[^1-2]%

于 2017-05-12T07:56:43.707 回答
0

在 Access SQL 中,我会使用它。我想 SQLserver 具有相同的语法。

select * from jobdetails where job_no like "0711*" or job_no like "0712*"

于 2009-07-02T19:36:32.887 回答
0

派对有点晚了,但你可以使用 STRING_SPLIT

SELECT jobdetails.* 
FROM jobdetails
CROSS APPLY  (select value 
              from STRING_SPLIT('540%,%144,orange,coconut',',') 
              WHERE jobdetails.job_no 
              like value) as leek

把它变成一个可重用的函数并不需要更多的工作

于 2021-10-06T09:44:10.450 回答
-1

Try this

select * 
from jobdetails 
where job_no between '0711' and '0713'

the only problem is that job '0713' is going to be returned as well so can use '07299999999999' or just add and job_no <> '0713'

Dan zamir

于 2010-02-07T14:08:04.240 回答
-1

您的问题中就有答案。使用 IN 时不能直接传递通配符。但是,您可以使用子查询。

尝试这个:

select *
from jobdetails
where job_no in (
select job_no
from jobdetails
where job_no like '0711%' or job_no like '0712%')
)

我知道这看起来很疯狂,因为您可以坚持在 WHERE 子句中使用 OR。为什么是子查询?但是,当您必须匹配来自不同来源的详细信息时,子查询方法将很有用。

拉吉

于 2009-07-02T19:13:55.740 回答
-1

我只是在学习这些东西,但这有用吗?

select *
from jobdetails
where job_no regexp "^(071)[1|2]";
于 2021-05-06T20:05:52.503 回答
-4

这可能是我最简单的解决方案使用like any

select *
from jobdetails
where job_no like any ('0711%', '0712%')

在 Teradata 中,这可以正常工作。

于 2015-09-28T13:52:41.957 回答