忽略ORDERBY子句中的引号、数字、空格等



我使用ORDER BY title ASC按标题对产品进行排序,得到:

"Some" title       // "
1 More title       // 1 
Another title      // A
Third title        // T
Yet another title  // Y

作为查询的一部分,我想忽略引号和其他非字母字符,这样它就会产生:

Another title      // A
1 More title       // M
"Some" title       // S
Third title        // T
Yet another title  // Y

有没有可能用Postgres去掉这些东西,或者作为查询的一部分进行预处理,或者我需要另一列?

更新

这非常有效:LOWER(regexp_replace(title, '[^[:alpha:]]', '', 'g')) ASC

此处提供更多选项:http://www.postgresql.org/docs/current/interactive/functions-matching.html#POSIX-等级-逃生台

一种方法是使用regexp_replace():

order by regexp_replace(title, '[^a-zA-Z]', '', 'g')

您也可以使用:

order by regexp_replace(title, '[^[:alpha:]]', '', 'g')

这对于非ASCII字符集更安全。

使用替换

 ORDER BY replace(title, '"', '') asc

更先进的是

ORDER BY regexp_replace(title, '[^a-zA-Z]', '', 'g')

您可以按字符串排序,将"替换为空字符串。数字字符也是如此。

SELECT *
FROM your_table
ORDER BY REPLACE(REPLACE(title, '"', ''), '1', '') ... ASC

使用regex_replace:

SELECT *
FROM your_table
ORDER BY regexp_replace(title, '[^A-Za-z]', '', 'g') ASC;

最新更新