Postgresql 模式匹配数字的字符串列
给定一个字符串形式的测量表(即“4.6 英寸,3 毫米”,“长度:4.66 英寸”等),我想检索基本上包含数字左侧或右侧任何内容的行,只要数字匹配。
换句话说,如果我正在搜索“6”:
- 匹配:“6 in”、“6in”、“asdf 6 in”等
- 不匹配:“16 in”、“6.6 in”、“with 66”到目前为止,
我有以下查询:
select distinct length from items where length ~ '[[:<:]]6[[:>:]]';
但这并不完全是我正在寻找的,因为这就是结果:
length
---------------
6.25 Inches
4.6666 Inches
6.5 Inches
1.66 Inches
6 Inches
6.75 Inches
6.71 Inches
24.6 Inches
3.6666 Inches
我正在使用 PostgreSQL 9。任何帮助将不胜感激。谢谢
Given a table of measurements as strings (i.e. "4.6 Inches, 3mm", "Length: 4.66in", etc), I'd like to retrieve rows with essentially anything left or right of the number, so long as the number matches.
In other words, if I'm searching for "6":
- match: "6 in", "6in", "asdf 6 in", etc
- don't match: "16 in", "6.6 in", "with 66 in", etc
I have the following query thus far:
select distinct length from items where length ~ '[[:<:]]6[[:>:]]';
But it's not quite what I'm looking for, as this is the result:
length
---------------
6.25 Inches
4.6666 Inches
6.5 Inches
1.66 Inches
6 Inches
6.75 Inches
6.71 Inches
24.6 Inches
3.6666 Inches
I'm using PostgreSQL 9. Any help would be appreciated. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
据我了解,您希望字符串中的数字完全匹配。因此数字(在您的例子中为 6)不能被数字或小数点包围。所以正则表达式应该如下所示:
您可以将 6, 更改为您要查找的任何数字。
As I understood, you want exact number match in string. So the number (6 in your case) cannot be surrounded by numerics or decimal sign. So the regex should look like that:
You can change the 6, to any number you are looking for.
怎么样:
How about :
已编辑以正确使用“6”和“6.6”。
Edited to work correctly with ' 6 ' and '6.6'.
无意冒犯,但将其存储为 varchar 并不是一个好主意。
这应该存储为两个单独的字段(数量/单位),或者(更好)单个字段(具有预定义单位的数量,例如毫米),并进行相应的查询。
No offense, but storing this as a varchar is not a good idea.
This should be stored as two separate fields (quantity / units), or (better yet) a single field (quantity with a predefined unit, e.g. mm), and queried accordingly.