SQL Server 在 IN 中使用通配符

发布于 2024-07-26 05:05:10 字数 597 浏览 3 评论 0原文

因为我相信这应该是一个基本问题,所以我知道这个问题可能已经被问过,但我无法找到它。 我可能即将获得“同伴压力”徽章,但无论如何我都会问:

在 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,但我知道必须有更好的方法。 你用什么方法来做这个?

Since I believe this should be a basic question I know this question has probably been asked, but I am unable to find it. I'm probably about to earn my Peer Pressure badge, but I'll ask anyway:

Is there a way in SQL Server that I am not aware of for using the wildcard character % when using IN.

I realize that I can use OR's like:

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

and in some cases I can use a subquery like:

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

but I'm looking to do something like the following:

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

In this case it uses the percent sign as a character instead of a wildcard character so no rows are returned. I currently just use a bunch of OR's when I have to do this, but I know there has to be a better way. What method do you use for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

遮云壑 2024-08-02 05:05:11

尝试一下,

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

唯一的问题是 job '0713' 也会被返回
因此可以使用 '07299999999999' 或仅添加 和 job_no <> '0713'

丹·扎米尔

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

萤火眠眠 2024-08-02 05:05:11

我刚刚学习这些东西,但这有用吗?

select *
from jobdetails
where job_no regexp "^(071)[1|2]";

I'm just learning this stuff but would this work?

select *
from jobdetails
where job_no regexp "^(071)[1|2]";
失与倦" 2024-08-02 05:05:11

这可能是最简单的解决方案,使用像任何一样

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

在 Teradata 中工作正常。

This might me the most simple solution use like any

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

In Teradata this works fine.

路还长,别太狂 2024-08-02 05:05:10

怎么样:

WHERE LEFT(job_no, 4) IN ('0711', '0712', ...)

How about:

WHERE LEFT(job_no, 4) IN ('0711', '0712', ...)
稚气少女 2024-08-02 05:05:10

我想我已经以简单的形式找到了本次调查的发起人想要的解决方案。 它对我有用,实际上这就是我来到这里的原因。 我相信只需在列周围使用括号(如 '%text%')并结合 OR 就可以了。

select * from tableName
where (sameColumnName like '%findThis%' or sameColumnName like '%andThis%' or 
sameColumnName like '%thisToo%' or sameColumnName like '%andOneMore%') 

I think I have a solution to what the originator of this inquiry wanted in simple form. It works for me and actually it is the reason I came on here to begin with. I believe just using parentheses around the column like '%text%' in combination with ORs will do it.

select * from tableName
where (sameColumnName like '%findThis%' or sameColumnName like '%andThis%' or 
sameColumnName like '%thisToo%' or sameColumnName like '%andOneMore%') 
静谧幽蓝 2024-08-02 05:05:10

像这样的事情怎么样?

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

How about something like this?

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
鲜血染红嫁衣 2024-08-02 05:05:10

您可以尝试这样的事情:

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

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

You could try something like this:

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

Not exactly what you're asking, but it has the same effect, and is flexible in other ways too :)

隐诗 2024-08-02 05:05:10
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
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
夜深人未静 2024-08-02 05:05:10

我有一个类似的目标 - 并得出了这个解决方案:

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

我假设您的各种代码('0711%','0712%'等),包括%,存储在一个表中,我称之为* table_of_codes*,带有字段代码

如果代码表中没有存储%,则只需连接“%”即可。 例如:

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

据我所知, concat() 函数可能会根据特定数据库的不同而有所不同。

我希望它有帮助。 我改编自:

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

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

聚集的泪 2024-08-02 05:05:10
  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
  1. I firstly added one off static table with ALL possibilities of my wildcard results
    (this company has a 4 character nvarchar code as their localities and they wildcard their locals)
    i.e. they may have 456? which would give them 456[1] to 456[Z] i.e 0-9 & a-z

  2. I had to write a script to pull the current user (declare them) and pull the masks for the declared user.

  3. Create some temporary tables just basic ones to rank the row numbers for this current user

  4. loop through each result (YOUR Or this Or that etc...)

  5. Insert into the test Table.

Here is the script I used:

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
尝蛊 2024-08-02 05:05:10

正如杰里米·史密斯(Jeremy Smith)发布的那样,我将重述一下,因为我无法回答他的那个特定问题。

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

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

As Jeremy Smith posted it, i'll recap, since I couldn't answer to that particular question of his.

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

If you just need 0711% and 0712% you can also place a ranges within the brackets. For the NOT keyword you could also use [^1-2]%

眼眸里的那抹悲凉 2024-08-02 05:05:10

IN 运算符只不过是“=”比较的奇特OR。 事实上,“只不过”是这样,在 SQL 2000 中,当列表包含大约 10k 条目时,由于将 IN 扩展为 OR,因此出现了堆栈溢出错误(是的,有人写了 10k IN 条目...)。 所以你不能在其中使用任何通配符匹配。

The IN operator is nothing but a fancy OR of '=' comparisons. In fact it is so 'nothing but' that in SQL 2000 there was a stack overflow bug due to expansion of the IN into ORs when the list contained about 10k entries (yes, there are people writing 10k IN entries...). So you can't use any wildcard matching in it.

撩起发的微风 2024-08-02 05:05:10

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

选择 *
来自职位详细信息
其中 job_no 如“0711*”或 job_no 如“0712*”

In Access SQL, I would use this. I'd imagine that SQLserver has the same syntax.

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

伴我心暖 2024-08-02 05:05:10

有点晚了,但你可以使用 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

将其变成可重用函数并不需要太多工作

A bit late to the party, but you could use 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

It wouldn't take very much more work to turn that into a reusable function

若沐 2024-08-02 05:05:10

我有同样的问题,并在 WHERE 子句中使用 SUBSTRING 函数找到了一个非常简单的解决方案。 这不会影响 job_no 在返回中的显示方式,而只是根据为子字符串函数输入的条件修剪 IN 搜索。

与 LEFT 或 RIGHT 相比,使用 SUBSTRING 的优点是您可以定义起始字符,因此如果您只想搜索字符串的中间,您可以这样做。

SELECT *
FROM jobdetails
WHERE SUBSTRING(job_no, 1, 4) IN ('0711', '0712')

I had this same question and found a very simple solution using the SUBSTRING function in the WHERE clause. This will not affect how the job_no displays in the return, rather it just trims the IN search down based on the criteria entered for the substring function.

The advantage of using SUBSTRING versus LEFT or RIGHT is that you can define the starting character so if you just want to search the middle of a string, you can do so.

SELECT *
FROM jobdetails
WHERE SUBSTRING(job_no, 1, 4) IN ('0711', '0712')
幸福丶如此 2024-08-02 05:05:10

你的问题里就有答案了。 使用 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 。 为什么是子查询? 然而,当您必须匹配来自不同来源的详细信息时,子查询方法将很有用。

拉吉

You have the answer right there in your question. You cannot directly pass wildcard when using IN. However, you can use a sub-query.

Try this:

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

I know that this looks crazy, as you can just stick to using OR in your WHERE clause. why the subquery? How ever, the subquery approach will be useful when you have to match details from a different source.

Raj

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文