简体   繁体   English

如何在MySQL查询中使用ORDER BY时忽略特殊字符

[英]How to ignore special characters when using ORDER BY in a MySQL query

I have the following MySQL query that provides data to a Python web page. 我有以下MySQL查询,它为Python网页提供数据。 On the web page, I have a list of song titles, and I want it to be alphabetized ignoring punctuation and spaces. 在网页上,我有一个歌曲标题列表,我希望它按字母顺序排列,忽略标点符号和空格。 My MySQL database is UTF-8 encoded, and some of the punctuation that needs to be ignored is special characters such as curly apostrophes, etc. 我的MySQL数据库是UTF-8编码的,需要忽略的一些标点符号是特殊字符,如撇号等。

SELECT * FROM Tracks\
JOIN Artists USING (ArtistID)\
JOIN Albums USING (AlbumID)\
JOIN Songs USING (SongID)\
ORDER BY UPPER(\
REPLACE(\
REPLACE(\
REPLACE(\
REPLACE(\
REPLACE(\
REPLACE(\
REPLACE(\
REPLACE(\
REPLACE(\
REPLACE(\
REPLACE(\
REPLACE(\
REPLACE(SoName, ' ', ''), /* space */\
                        ',', ''), /* comma */\
                        '.', ''), /* period */\
                        ':', ''), /* colon */\
                        ';', ''), /* semicolon */\
                        '!', ''), /* exclamation point */\
                        '?', ''), /* question mark */\
                   '\u201c', ''), /* left curly double quote */\
                   '\u201d', ''), /* right curly double quote */\
                   '\u2019', ''), /* right curly single quote (apostrophe) */\
                   '\u2013', ''), /* n-dash */\
                   '\u2014', ''), /* m-dash */\
                   '\u2026', '') /* ellipsis */), (SongID), UPPER(AlTitle)

The REPLACE in my query seems to work perfectly for the non-special characters, like the space, comma, period, etc., but it seems to skip over the special characters. 我的查询中的REPLACE似乎非常适用于非特殊字符,如空格,逗号,句点等,但它似乎跳过了特殊字符。

My guess is that the characters need to be written in a different format. 我的猜测是角色需要以不同的格式编写。 I tried the following with no success: REPLACE(SoName, '\…', '') REPLACE(SoName, u'\\2026', '') REPLACE(SoName, 0xE280A6, '') ... 我尝试了以下但没有成功: REPLACE(SoName, '\…', '') REPLACE(SoName, u'\\2026', '') REPLACE(SoName, 0xE280A6, '') ...

MySQL string literals do not provide an escape sequence for multi-byte characters. MySQL字符串文字不提供多字节字符的转义序列。 This has been a feature request for over 7 years and is still awaiting triage: I wouldn't hold my breath that it will be resolved any time soon. 这是一个超过7年的功能要求 ,仍在等待分类:我不会屏住呼吸,它将很快得到解决。

You must either put the actual character in your string literal, or else know its constituent bytes in your desired encoding (in which case you could then use something like CHAR() ). 您必须将实际字符放在字符串文字中,或者以您想要的编码知道其组成字节(在这种情况下,您可以使用类似CHAR() )。

My brother told me to put this at the very top of the Python page that contains the MySQL query: 我的兄弟告诉我把它放在包含MySQL查询的Python页面的顶部:

from __future__ import unicode_literals

Everything seems to work now! 一切似乎现在都有效!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM