MySql 不像正则表达式?
我正在尝试查找第一个字符不是数字的行。我有这个:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您的查询中有一个小错误。它应该是:
注意双方括号。您还可以将其重写为以下形式,以避免也匹配空字符串:
另请注意,使用 REGEXP 会阻止使用索引,并将导致表扫描或索引扫描。如果您想要更高效的查询,您应该尝试在可能的情况下不使用 REGEXP 重写查询:
然后在 (qkey, action) 上添加索引。虽然读起来不太愉快,但它应该能提供更好的性能。如果每个 qkey 只有少量操作,那么它可能不会带来任何显着的性能提升,因此您可以坚持使用更简单的查询。
First there is a slight error in your query. It should be:
Note the double square parentheses. You could also rewrite it as the following to avoid also matching the empty string:
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:
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.
您当前的正则表达式将匹配由恰好一个数字组成的值,而不仅仅是第一个字符。只需从其末尾删除
$
即可,这意味着“值结束”。它只会检查第一个字符,除非您告诉它检查更多字符。^[: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".