国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 數(shù)據(jù)庫(kù) > MySQL > 正文

MySQL中DATE_FORMATE函數(shù)內(nèi)置字符集解析

2024-07-24 12:36:44
字體:
供稿:網(wǎng)友

今天在利用DATE_FORMATE處理兩個(gè)日期判斷時(shí)發(fā)現(xiàn)提示 Illegal mix of collations (utf8_general_ci,COERCIBLE),這個(gè)是字符集問題呀,但我是日期怎么會(huì)這樣,當(dāng)時(shí)沒看到,后來看到一篇文章才想起來,下面我們一起來看看解決辦法.

處理一個(gè)SQL,簡(jiǎn)化過后的執(zhí)行報(bào)錯(cuò),代碼如下:

  1. mysql> select date_format('2013-11-19','Y-m-d') > timediff('2013-11-19''2013-11-20');                            
  2.  
  3. ERROR 1267 (HY000): Illegal mix of collations (utf8_general_ci,COERCIBLE) and (latin1_swedish_ci,NUMERICfor operation '>' 

乍一看挺莫名其妙的,查了下手冊(cè),發(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é)果是帶有字符集/校驗(yàn)集屬性的,而 TIMEDIFF() 函數(shù)則沒有字符集/校驗(yàn)集屬性,我們來驗(yàn)證一下,代碼如下:

  1. mysql> set names utf8; 
  2. mysql> select charset(date_format('2013-11-19','Y-m-d')), charset(timediff('2013-11-19''2013-11-20')); 
  3. +--------------------------------------------+-----------------------------------------------+ 
  4. | charset(date_format('2013-11-19','Y-m-d')) | charset(timediff('2013-11-19''2013-11-20')) | 
  5. +--------------------------------------------+-----------------------------------------------+ 
  6. | utf8                                       | binary                                        | 
  7. +--------------------------------------------+-----------------------------------------------+ 
  8.  
  9. mysql> set names gb2312; 
  10. mysql> select charset(date_format('2013-11-19','Y-m-d')), charset(timediff('2013-11-19''2013-11-20')); 
  11. +--------------------------------------------+-----------------------------------------------+ 
  12. | charset(date_format('2013-11-19','Y-m-d')) | charset(timediff('2013-11-19''2013-11-20')) | 
  13. +--------------------------------------------+-----------------------------------------------+//開源代碼Vevb.com 
  14. | gb2312                                     | binary                                        | 
  15. +--------------------------------------------+-----------------------------------------------+ 

可以看到,隨著通過 SET NAMES 修改 character_set_connection、collation_connection  值,DATE_FORMAT() 函數(shù)返回結(jié)果的字符集也跟著不一樣,在這種情況下,想要正常工作,就需要將結(jié)果進(jìn)行一次字符集轉(zhuǎn)換,代碼如下:

  1. mysql> select date_format('2013-11-19','Y-m-d') > convert(timediff('2013-11-19', '2013-11-20') using utf8); 
  2. +----------------------------------------------------------------------------------------------+ 
  3. | date_format('2013-11-19','Y-m-d') > convert(timediff('2013-11-19', '2013-11-20') using utf8) | 
  4. +----------------------------------------------------------------------------------------------+ 
  5. |                                                                                            1 | 
  6. +----------------------------------------------------------------------------------------------+  

就可以了.

P.S,MySQL的版本:5.5.20-55-log Percona Server (GPL), Release rel24.1,Revision 217.

附后,下面是函數(shù)的參數(shù)說明:

%S, %s 兩位數(shù)字形式的秒( 00,01, . . ., 59) 
%i 兩位數(shù)字形式的分( 00,01, . . ., 59) 
%H 兩位數(shù)字形式的小時(shí),24 小時(shí)(00,01, . . ., 23) 
%h, %I 兩位數(shù)字形式的小時(shí),12 小時(shí)(01,02, . . ., 12) 
%k 數(shù)字形式的小時(shí),24 小時(shí)(0,1, . . ., 23) 
%l 數(shù)字形式的小時(shí),12 小時(shí)(1, 2, . . ., 12) 
%T 24 小時(shí)的時(shí)間形式(hh : mm : s s) 
%r 12 小時(shí)的時(shí)間形式(hh:mm:ss AM 或hh:mm:ss PM) 
%p AM 或P M 
%W 一周中每一天的名稱( Sunday, Monday, . . ., Saturday) 
%a 一周中每一天名稱的縮寫( Sun, Mon, . . ., Sat) 
%d 兩位數(shù)字表示月中的天數(shù)( 00, 01, . . ., 31) 
%e 數(shù)字形式表示月中的天數(shù)( 1, 2, . . ., 31) 
%D 英文后綴表示月中的天數(shù)( 1st, 2nd, 3rd, . . .) 
%w 以數(shù)字形式表示周中的天數(shù)( 0 = Sunday, 1=Monday, . . ., 6=Saturday) 
%j 以三位數(shù)字表示年中的天數(shù)( 001, 002, . . ., 366) 
% U 周(0, 1, 52),其中Sunday 為周中的第一天 
%u 周(0, 1, 52),其中Monday 為周中的第一天 
%M 月名(January, February, . . ., December) 
%b 縮寫的月名( January, February, . . ., December) 
%m 兩位數(shù)字表示的月份( 01, 02, . . ., 12) 
%c 數(shù)字表示的月份( 1, 2, . . ., 12) 
%Y 四位數(shù)字表示的年份 
%y 兩位數(shù)字表示的年份 
%% 直接值“%”

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 堆龙德庆县| 连山| 西昌市| 宁津县| 望谟县| 江达县| 石门县| 龙陵县| 淮安市| 台南市| 天长市| 弥渡县| 镇坪县| 犍为县| 灵台县| 江山市| 黑河市| 习水县| 松滋市| 岳普湖县| 湘潭市| 芒康县| 秀山| 武陟县| 东乡族自治县| 木兰县| 苍南县| 高要市| 屯昌县| 德庆县| 高阳县| 南平市| 阿克苏市| 和政县| 连平县| 镇坪县| 乌恰县| 铜陵市| 偏关县| 兴隆县| 偏关县|