[英]Sort SQL ORDER BY query without slashes in front of escaped characters
I am trying to figure out how to sort an SQL query by Last Name. 我试图弄清楚如何按姓氏对SQL查询进行排序。 This is working except in the case of where a customers last name has a special character.
除客户姓氏具有特殊字符外,此方法均有效。
For example O'Maley. 例如O'Maley。
If this last name is sorted with a last name of "Omar" for example the order should be: 例如,如果此姓氏以“ Omar”的姓氏排序,则顺序应为:
O'Maley Omar 奥马里·奥马尔
BUT since I use addslashes() in PHP before adding it into the database the sort is actually happening on O\\'Maley and so the sort ends up being. 但是,因为我在将PHP添加到数据库中之前在PHP中使用了addlashes(),所以排序实际上是在O''Maley上进行的,因此排序最终得以实现。
Omar O'Maley 奥马尔·奥马里(Omar O'Maley)
Of course the \\ is removed with stripslashes() before being displayed to the user. 当然,在显示给用户之前,先用stripslashes()删除\\。
How can I make this properly sort? 如何进行正确排序?
Look into using either the REPLACE()
or TRANSLATE()
sql functions. 研究使用
REPLACE()
或TRANSLATE()
sql函数。 For example: 例如:
ORDER BY REPLACE(REPLACE(last_name, '\\', ''), ''', '')
The above REPLACE()
functions will remove the backslashes and single quotes before doing the order by. 上面的
REPLACE()
函数将在进行排序之前删除反斜杠和单引号。
ORDER BY TRANSLATE(last_name, '\\'', '')
The above TRANSLATE()
function will also remove the backslashes and single quotes. 上面的
TRANSLATE()
函数还将删除反斜杠和单引号。 This might be a better format for specifying many more characters to remove from a string. 这可能是一种更好的格式,用于指定要从字符串中删除的更多字符。
Note: You don't mention a database, so I'm assuming Oracle. 注意:您没有提到数据库,所以我假设使用Oracle。 Most databases have equivalent functions.
大多数数据库具有等效功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.