选择逗号分隔列表项的数量

发布于 2024-10-10 06:56:30 字数 69 浏览 0 评论 0原文

我有一列包含逗号分隔值,我需要选择包含 13 个逗号的所有行。它们分隔数字,因此我无需担心任何包含逗号的字符串。我该怎么做?

I have a column that has comma seperated values and I need to select all rows that have 13 commas. They seperate numbers so I don't need to worry about any strings that contain commas. How would I do this?

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

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

发布评论

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

评论(4

烟火散人牵绊 2024-10-17 06:56:30

为了

select * from table
where length(replace(your_column, ',', ''))=length(your_column)-13;

更好地利用索引,您应该寻求标准化您的表

alternative to like (I do not like the like, and the above will fail if contains 14 commas or more)

select * from table
where length(replace(your_column, ',', ''))=length(your_column)-13;

for better utilize the index, you should seek to normalize your table

幸福不弃 2024-10-17 06:56:30

如果您使用 PostgreSQL,您还可以使用正则表达式。

但是,更好的问题可能是为什么只有一个包含逗号分隔值的列而不是多个列。

If you're using PostgreSQL, you could also use regular expressions.

However, a better question might be why you have a single column with comma-separated values instead of multiple columns.

待"谢繁草 2024-10-17 06:56:30

如果将包含 14 个逗号的字符串算作包含 13 个逗号,那么这将起作用:

SELECT * FROM table WHERE column LIKE '%,%,%,%,%,%,%,%,%,%,%,%,%,%'

% 匹配任何字符串(包括零长度)。

If you count a string with 14 commas as having 13 commas, then this will work:

SELECT * FROM table WHERE column LIKE '%,%,%,%,%,%,%,%,%,%,%,%,%,%'

% matches any string (including zero length).

他是夢罘是命 2024-10-17 06:56:30

在 PostgreSQL 中你可以这样做:

select col from table where length(regexp_replace(col, '[^,]', '', 'g')) = 13;

In PostgreSQL you can do:

select col from table where length(regexp_replace(col, '[^,]', '', 'g')) = 13;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文