SQL Server 在 IN 中使用通配符 [英] SQL Server using wildcard within IN
问题描述
因为我认为这应该是一个基本问题,所以我知道可能有人问过这个问题,但我找不到它.我可能即将获得 Peer Pressure 徽章,但我还是会问:
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:
在 SQL Server 中是否有一种我不知道在使用 IN 时使用通配符 % 的方法.
Is there a way in SQL Server that I am not aware of for using the wildcard character % when using IN.
我意识到我可以使用 OR 之类的:
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)
但我想做如下事情:
select *
from jobdetails
where job_no in ('0711%', '0712%')
在这种情况下,它使用百分号作为字符而不是通配符,因此不会返回任何行.我目前只在必须这样做时使用一堆 OR,但我知道必须有更好的方法.你用什么方法来做到这一点?
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?
推荐答案
怎么样:
WHERE LEFT(job_no, 4) IN ('0711', '0712', ...)
这篇关于SQL Server 在 IN 中使用通配符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!