MySQL中DATE_FORMATE函數(shù)使用時的注意點
今天幫同事處理一個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ù)返回的結果是帶有字符集/校驗集屬性的,而 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ù)返回結果的字符集也跟著不一樣。在這種情況下,想要正常工作,就需要將結果進行一次字符集轉(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 | +----------------------------------------------------------------------------------------------+
就可以了 :)
相關文章
Mysql 8 新特性 window functions 的作用
MySQL是眾多網(wǎng)站技術棧中的標準配置,是廣受歡迎的開源數(shù)據(jù)庫,已經(jīng)推出了8.0的第一個候選發(fā)行版本。接下來通過本文給大家分享Mysql 8 新特性 window functions 的作用,需要的朋友參考下吧2017-11-11(MariaDB)MySQL數(shù)據(jù)類型和存儲機制全面講解
下面小編就為大家分享一篇(MariaDB)MySQL數(shù)據(jù)類型和存儲機制全面講解,具有很的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01MySQL MaxCompute與AnalyticDB實現(xiàn)數(shù)據(jù)處理與轉(zhuǎn)換過程詳解
AnalyticDB MySQL(簡稱ads)與 MaxCompute(簡稱odps)進行數(shù)據(jù)轉(zhuǎn)換時,個別語法有差別,記錄下來,方便備查,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧2022-12-12MySQL查詢優(yōu)化:連接查詢排序limit(join、order by、limit語句)介紹
兩張表連接查詢并limit,SQL效率很高,但是加上order by以后,語句的執(zhí)行時間變的巨長,效率巨低,接下來為大家介紹下連接查詢排序limit2013-04-04