MySql 不像正则表达式?

发布于 2024-08-28 06:13:05 字数 184 浏览 5 评论 0原文

我正在尝试查找第一个字符不是数字的行。我有这个:

SELECT DISTINCT(action) FROM actions 
WHERE qkey = 140 AND action NOT REGEXP '^[:digit:]$';

但是,我不确定如何确保它只检查第一个字符......

I'm trying to find rows where the first character is not a digit. I have this:

SELECT DISTINCT(action) FROM actions 
WHERE qkey = 140 AND action NOT REGEXP '^[:digit:]

But, I'm not sure how to make sure it checks just the first character...

;

But, I'm not sure how to make sure it checks just the first character...

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

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

发布评论

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

评论(2

许仙没带伞 2024-09-04 06:13:05

首先,您的查询中有一个小错误。它应该是:

NOT REGEXP '^[[:digit:]]'

注意双方括号。您还可以将其重写为以下形式,以避免也匹配空字符串:

REGEXP '^[^[:digit:]]'

另请注意,使用 REGEXP 会阻止使用索引,并将导致表扫描或索引扫描。如果您想要更高效的查询,您应该尝试在可能的情况下不使用 REGEXP 重写查询:

SELECT DISTINCT(action) FROM actions 
WHERE qkey = 140 AND action < '0'
UNION ALL
SELECT DISTINCT(action) FROM actions 
WHERE qkey = 140 AND action >= ':'

然后在 (qkey, action) 上添加索引。虽然读起来不太愉快,但它应该能提供更好的性能。如果每个 qkey 只有少量操作,那么它可能不会带来任何显着的性能提升,因此您可以坚持使用更简单的查询。

First there is a slight error in your query. It should be:

NOT REGEXP '^[[:digit:]]'

Note the double square parentheses. You could also rewrite it as the following to avoid also matching the empty string:

REGEXP '^[^[:digit:]]'

Also note that using REGEXP prevents an index from being used and will result in a table scan or index scan. If you want a more efficient query you should try to rewrite the query without using REGEXP if it is possible:

SELECT DISTINCT(action) FROM actions 
WHERE qkey = 140 AND action < '0'
UNION ALL
SELECT DISTINCT(action) FROM actions 
WHERE qkey = 140 AND action >= ':'

Then add an index on (qkey, action). It's not as pleasant to read, but it should give better performance. If you only have a small number of actions for each qkey then it probably won't give any noticable performance increase so you can stick with the simpler query.

街角卖回忆 2024-09-04 06:13:05

您当前的正则表达式将匹配由恰好一个数字组成的值,而不仅仅是第一个字符。只需从其末尾删除 $ 即可,这意味着“值结束”。它只会检查第一个字符,除非您告诉它检查更多字符。

^[:digit:] 可以使用,这意味着“值的开头,后跟一位数字”。

Your current regex will match values consisting of exactly one digit, not the first character only. Just remove the $ from the end of it, that means "end of value". It'll only check the first character unless you tell it to check more.

^[:digit:] will work, that means "start of the value, followed by one digit".

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