MySQL中日期比較時遇到的編碼問題解決辦法
今天幫同事處理一個SQL(簡化過后的)執(zhí)行報錯:
mysql> select date_format('2013-11-19','Y-m-d') > timediff('2013-11-19', '2013-11-20');
ERROR 1267 (HY000): Illegal mix of collations (utf8_general_ci,COERCIBLE) and (latin1_swedish_ci,NUMERIC) for operation '>'
乍一看挺莫名其妙的,查了下手冊,發(fā)現(xiàn)有這么一段:
The language used for day and month names and abbreviations is controlled by the value of the lc_time_names system variable (Section 9.7, “MySQL Server Locale Support”).
The DATE_FORMAT() returns a string with a character set and collation given by character_set_connection and collation_connection so that it can return month and weekday names containing non-ASCII characters.
也就是說,DATE_FORMATE() 函數(shù)返回的結(jié)果是帶有字符集/校驗集屬性的,而 TIMEDIFF() 函數(shù)則沒有字符集/校驗集屬性,我們來驗證一下:
mysql> set names utf8;
mysql> select charset(date_format('2013-11-19','Y-m-d')), charset(timediff('2013-11-19', '2013-11-20'));
+--------------------------------------------+-----------------------------------------------+
| charset(date_format('2013-11-19','Y-m-d')) | charset(timediff('2013-11-19', '2013-11-20')) |
+--------------------------------------------+-----------------------------------------------+
| utf8 | binary |
+--------------------------------------------+-----------------------------------------------+
mysql> set names gb2312;
mysql> select charset(date_format('2013-11-19','Y-m-d')), charset(timediff('2013-11-19', '2013-11-20'));
+--------------------------------------------+-----------------------------------------------+
| charset(date_format('2013-11-19','Y-m-d')) | charset(timediff('2013-11-19', '2013-11-20')) |
+--------------------------------------------+-----------------------------------------------+
| gb2312 | binary |
+--------------------------------------------+-----------------------------------------------+
可以看到,隨著通過 SET NAMES 修改 character_set_connection、collation_connection 值,DATE_FORMAT() 函數(shù)返回結(jié)果的字符集也跟著不一樣。在這種情況下,想要正常工作,就需要將結(jié)果進(jìn)行一次字符集轉(zhuǎn)換,例如:
mysql> select date_format('2013-11-19','Y-m-d') > convert(timediff('2013-11-19', '2013-11-20') using utf8);
+----------------------------------------------------------------------------------------------+
| date_format('2013-11-19','Y-m-d') > convert(timediff('2013-11-19', '2013-11-20') using utf8) |
+----------------------------------------------------------------------------------------------+
| 1 |
+----------------------------------------------------------------------------------------------+
就可以了
P.S,MySQL的版本:5.5.20-55-log Percona Server (GPL), Release rel24.1, Revision 217
相關(guān)文章
Mysql數(shù)據(jù)庫表中為什么有索引卻沒有提高查詢速度
你有沒有想起過為什么明明再數(shù)據(jù)庫中有索引,但是查詢速度卻并沒有希望的那樣快?本篇文章將帶給你答案,跟小編一起看看吧2022-02-02mybatis-plus分頁傳入?yún)?shù)后sql where條件沒有l(wèi)imit分頁信息操作
這篇文章主要介紹了mybatis-plus分頁傳入?yún)?shù)后sql where條件沒有l(wèi)imit分頁信息操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11MYSQL?SQL查詢近7天一個月的數(shù)據(jù)的操作方法
這篇文章主要介紹了MYSQL?SQL查詢近7天一個月的數(shù)據(jù)的操作方法,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04mysql 遞歸查找菜單節(jié)點(diǎn)的所有子節(jié)點(diǎn)的方法
這篇文章主要介紹了mysql 遞歸查找菜單節(jié)點(diǎn)的所有子節(jié)點(diǎn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11