[英]MySQL Regexp won't work
I have the following query: 我有以下查询:
SELECT `admin_users`.*
FROM `admin_users`
WHERE (avatar REGEXP 'avatars/Name-[0-9]+.jpg+')
ORDER BY `admin_users`.`avatar`
DESC LIMIT 1
It's ok if I have something like: 如果我有类似的东西也可以:
avatars/Name-5.jpg
avatars/Name-6.jpg
But if I have, avatars/Name-15.jpg
, for example, it doesn't return in query. 但是,如果有
avatars/Name-15.jpg
,例如,它不会在查询中返回。
In other words, It only works for 1 digit, not for more. 换句话说,它仅适用于1位数,而不适用于更多位数。 How can I solve it?
我该如何解决?
When comparing strings (an that is what avatar
is), "avatars/Name-1..." comes before "avatars/Name-5..." simply because the string "1" comes before "5". 比较字符串(
avatar
是什么)时,“化身/名称-1 ...”位于“化身/名称-5 ...”之前,仅是因为字符串 “ 1”位于“ 5”之前。
It is not practical to order those by an embedded number. 用一个嵌入式数字对它们进行排序是不现实的。 This would do what you want, but it is pretty cryptic:
这会做您想要的,但是很神秘:
ORDER BY 0 + MID(avatar, 14)
To explain 解释
0+'abc'
will deliver 0
, since there is nothing at the beginning of abc
that looks like a number.) 0+'abc'
将传递0
,因为abc
开头没有看起来像数字的东西。) If the left part were not exactly 14 characters in all cases, the trick will fail. 如果在所有情况下左侧部分都不都是14个字符,则该技巧将失败。 And it may get so complicated as to be 'impossible' in SQL.
而且它可能变得如此复杂以至于在SQL中“不可能”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.