为什么在MySQL中排序时reg表达式匹配不起作用?

为什么在MySQL中排序时reg表达式匹配不起作用?,mysql,sql,Mysql,Sql,这个问题可以被认为是与……有关的。如果我有同一张桌子: 身份证件 名称 标志 1. 艾希礼 81 2. 萨曼莎 75 3. 贝尔维特 84 4. 朱莉娅 76 在甲骨文中,它确实对我有用 with students (id, name, marks) as( select 1, 'Ashley', 81 from dual union all select 2, 'Samantha', 75 from dual union all select 3, 'Belvet', 8

这个问题可以被认为是与……有关的。如果我有同一张桌子:

身份证件 名称 标志 1. 艾希礼 81 2. 萨曼莎 75 3. 贝尔维特 84 4. 朱莉娅 76
在甲骨文中,它确实对我有用

with students (id, name, marks) as(
select 1,   'Ashley',  81 from dual union all
select 2,   'Samantha',    75 from dual union all
select 3,   'Belvet',  84 from dual union all
select 4,   'Julia' ,  76 from dual)
SELECT
    regexp_substr(name, '[a-z]{3}$')
FROM
    students
ORDER BY
    regexp_substr(name, '[a-z]{3}$');

所有值都与模式匹配


dbfiddle

将Name regexp[a-z]{3}$添加到选择列表中,并查看返回的内容。存在order by问题时的标准技巧。它不会返回任何错误,但会返回一个新列,其值为1。REGEXP返回字符串expr的子字符串的起始索引,该子字符串与模式pat指定的正则表达式匹配,如果不匹配,则返回0。它不返回字符串。您正在查找REGEXP_SUBSTR@ontrack REGEXP返回字符串expr的子字符串的起始索引,该字符串与模式pat指定的正则表达式匹配,如果不匹配,则返回0。谁告诉你的???我认为REGEXP匹配整个字符串模式。在MySQL中使用类似的查询会给出错误REGEXP_SUBSTR不存在:从学生中选择REGEXP_SUBSTRName,[a-z]{3}$,其中标记>75按REGEXP_SUBSTRName排序,[a-z]{3}$ASC;好的,开始工作了。order by子句中的regexp_substr。“Ashley”应该排在第一位,因为“ley”<“lia”。@shiv_90如果是这样,则不需要regexp。
with students (id, name, marks) as(
select 1,   'Ashley',  81 from dual union all
select 2,   'Samantha',    75 from dual union all
select 3,   'Belvet',  84 from dual union all
select 4,   'Julia' ,  76 from dual)
SELECT *, 
       Name regexp "[a-z]{3}$" AS ordering_expression 
FROM students
ORDER BY Name regexp "[a-z]{3}$" ASC;
id | name | marks | ordering_expression -: | :------- | ----: | ------------------: 1 | Ashley | 81 | 1 2 | Samantha | 75 | 1 3 | Belvet | 84 | 1 4 | Julia | 76 | 1
-- test 6-letter pattern
with students (id, name, marks) as(
select 1,   'Ashley',  81 from dual union all
select 2,   'Samantha',    75 from dual union all
select 3,   'Belvet',  84 from dual union all
select 4,   'Julia' ,  76 from dual)
SELECT *, 
       Name regexp "[a-z]{6}$" AS ordering_expression 
FROM students
ORDER BY Name regexp "[a-z]{6}$" ASC;
id | name | marks | ordering_expression -: | :------- | ----: | ------------------: 4 | Julia | 76 | 0 1 | Ashley | 81 | 1 2 | Samantha | 75 | 1 3 | Belvet | 84 | 1
with students (id, name, marks) as(
select 1,   'Ashley',  81 from dual union all
select 2,   'Samantha',    75 from dual union all
select 3,   'Belvet',  84 from dual union all
select 4,   'Julia' ,  76 from dual)
SELECT *
FROM students
ORDER BY RIGHT(Name, 3) ASC;
id | name | marks -: | :------- | ----: 1 | Ashley | 81 4 | Julia | 76 2 | Samantha | 75 3 | Belvet | 84