MySQL數據庫:SQL語句中不可省去的引號

有一次在代碼中發現了一個BUG,在排查過程當中,差點暈翻;做一個DBA也碰到這種低級錯誤;大爲自嘲。其實這個問題在官方手冊裏就寫得很明白;(可見官方文檔的重要性) mysql

mysql >   create   table  h1 (id  int  , col1  char ( 1 ));
Query OK, 
0  rows affected ( 0.00  sec)

mysql
>   insert   into  h1  select   1 ,’I ' ;
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into h1 select 2,’F
' ;
Query OK, 
1  row affected ( 0.00  sec)
Records: 
1   Duplicates:  0   Warnings:  0

mysql
>   insert   into  h1  select   2 ,’ 0 ′;
Query OK, 
1  row affected ( 0.00  sec)
Records: 
1   Duplicates:  0   Warnings:  0

 

mysql >   select   *   from  h1 ;
+ —— + —— +
|  id    |  col1  |
+ —— + —— +
|      1   |  I     |
|      2   |  F     |
|      2   |   0      |
+ —— + —— +
3  rows  in   set  ( 0.00  sec)

mysql
>   select   *   from  h1  where  col1 = 0 ;
+ —— + —— +
|  id    |  col1  |
+ —— + —— +
|      1   |  I     |
|      2   |  F     |
|      2   |   0      |
+ —— + —— +
3  rows  in   set 2  warnings ( 0.00  sec)

mysql
>   select   *   from  h1  where  col1 = 0 ′;
+ —— + —— +
|  id    |  col1  |
+ —— + —— +
|      2   |   0      |
+ —— + —— +
1  row  in   set  ( 0.00  sec)

 

mysql >   select   *   from  h1  where  col1 = 1 ′;
Empty 
set  ( 0.00  sec)

mysql
>   select   *   from  h1  where  col1 = 1 ;
Empty 
set 2  warnings ( 0.00  sec)

  發現沒有: sql

  col1=0 ; 全部行被選中; spa

  col1=’0′ ; 結果集正常 文檔

mysql >   select   *   from  h1  where  col1 = 1 ′;
Empty 
set  ( 0.00  sec)

mysql
>   select   *   from  h1  where  col1 = 1 ;
Empty 
set 2  warnings ( 0.00  sec)

  想知道爲何嗎? table

  看這裏. select

mysql >  show warnings;
+ ——— + —— + ————————————— +
|   Level     |  Code  |  Message                                |
+ ——— + —— + ————————————— +
|  Warning  |   1292   |  Truncated incorrect  DOUBLE  value: ‘I’  |
|  Warning  |   1292   |  Truncated incorrect  DOUBLE  value: ‘F’  |
+ ——— + —— + ————————————— +
2  rows  in   set  ( 0.00  sec)

  MySQL當數據類型不匹配時,尊重用戶的輸入,將字段的類型作了隱式轉換而後來匹配,而字符向數字轉,結果都是0; 數據類型

相關文章
相關標籤/搜索