gpt4 book ai didi

sql - PostgreSQL : Pattern matching of values starting with "IR"

转载 作者:行者123 更新时间:2023-12-05 09:21:40 24 4
gpt4 key购买 nike

如果我有如下所示的表格内容:

id | value
------------
1 |CT 6510
2 |IR 52
3 |IRAB
4 |IR AB
5 |IR52

我只需要获取内容以“IR”开头然后是数字(忽略空格)的那些行。这意味着我应该得到值:

2  |IR 52
5 |IR52

因为它以“IR”开头,下一个非空格字符是一个整数。与 IRAB 不同,它也以“IR”开头,但“A”是下一个字符。我只能查询所有以 IR 开头的内容。但其他 IR 也在出现。

select * from public.record where value ilike 'ir%'

我该怎么做?谢谢。

最佳答案

您可以使用运算符~,它执行正则表达式匹配。例如:

SELECT * from public.record where value ~ '^IR ?\d';

添加星号以执行不区分大小写的匹配。

SELECT * from public.record where value ~* '^ir ?\d';

这些符号的意思是:

^: 字符串的开始

?:前面的字符(这里是一个空格)是可选的

\d:所有数字,相当于[0-9]

有关详细信息,请参阅:Regular Expression Match Operators

另见这个问题,信息量很大:difference-between-like-and-in-postgres

关于sql - PostgreSQL : Pattern matching of values starting with "IR",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30748205/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com