SQL-合并多个类似的查询 [英] SQL - Combining multiple like queries
问题描述
嘿,关于SO的第一个问题!任何人...
Hey my first question on SO! Anywho...
在SQL领域还是一个相对较新的人,所以我认为我可能在这里遗漏了一些东西。我的问题是我目前有一张充满电话号码的表。我想在查询中搜索与列表相似的电话号码。因此,例如,我要查找以 555123, 555321和 555987开头的电话号码。我通常知道,如果您有数字列表,则可以执行查询,例如
Still relatively a newb at SQL so I think I might be missing something here. My question is I currently have a table full of phone numbers. I want to have a query where I search for phone numbers that are similar to a list I have. So for example, I want to find phone numbers that begin with '555123', '555321', and '555987'. I know normally if you have a list of numbers you could just do a query such as
SELECT *
FROM phonenumbers
WHERE number in ('5551234567', '5559876543', .... );
有没有办法做到这一点?例如
Is there a way to do this with like? Such as
SELECT *
FROM phonenumbers
WHERE number in like ('555123%', '555321%', '555987%'); //I know this doesn't actually work
而不是必须单独执行
SELECT *
FROM phonenumbers
WHERE number like '555123%'
or number like '555321%'
or number like '555987%'; //Which does work but takes a long time
或者这样做更容易吗?我只是想念?我使用的是postgres,我不知道是否有任何命令可以帮助解决这个问题。谢谢!
Or is there an easier to do this that I'm just missing? I'm using postgres, I don't know if there's any commands it has that would help out with that. Thanks!
推荐答案
您可以使用 SIMILAR TO
并使用|管道'555123%| 555321%| 555987%'
You can use SIMILAR TO
and separate the tags with | pipe '555123%|555321%|555987%'
例如:
SELECT *
FROM phonenumbers
WHERE number SIMILAR TO '555123%|555321%|555987%'
这篇关于SQL-合并多个类似的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!