sqlite:在模式列表中的地球

发布于 2025-01-26 02:43:53 字数 615 浏览 1 评论 0原文

我有一个glob模式(未知长度)的列表,并且希望选择所有与模式的任何 任何条目。

天真的方法是:

SELECT name, age
WHERE name GLOB 'John*' OR name GLOB 'Mar*'
;

但是,这似乎非常笨拙(尤其是有很多模式)。

进行确切的匹配时,我可以在关键字中使用,例如:

SELECT name, age
WHERE name IN ('John', 'Mary')
;

我想知道,我是否可以使用glob,例如这样(显然是非 -工作示例):

SELECT name, age WHERE name GLOB IN ('John*', 'Mar*');

I have a list of GLOB patterns (of unknown length), and would like to SELECT all entries that match any of the patterns.

The naive approach would be:

SELECT name, age
WHERE name GLOB 'John*' OR name GLOB 'Mar*'
;

However, this seems extraordinarily clumsy (esp. if there are many patterns).

When doing an exact match, I can use the IN keyword, like so:

SELECT name, age
WHERE name IN ('John', 'Mary')
;

So I wonder, whether I can do something similar with GLOB, e.g. like this (obviously non-working example):

SELECT name, age WHERE name GLOB IN ('John*', 'Mar*');

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

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

发布评论

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

评论(1

皓月长歌 2025-02-02 02:43:53

使用CTE返回所需的所有模式,并且在主要查询中使用存在以检查是否有任何匹配:

WITH cte(pattern) AS (VALUES ('John*'), ('Mar*'))
SELECT t.*
FROM tablename t
WHERE EXISTS (SELECT 1 FROM cte c WHERE t.name GLOB c.pattern);

存在在找到第一个匹配时返回并且不需要检查所有模式。

请参阅

Use a CTE that returns all the patterns that you want and in the main query use EXISTS to check if there is any match:

WITH cte(pattern) AS (VALUES ('John*'), ('Mar*'))
SELECT t.*
FROM tablename t
WHERE EXISTS (SELECT 1 FROM cte c WHERE t.name GLOB c.pattern);

EXISTS returns as soon as it finds the 1st match and does not need to check all the patterns.

See the demo.

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