歡迎訪(fǎng)問(wèn)世紀網(wǎng)絡(luò )官網(wǎng),寶雞網(wǎng)站建設SEO優(yōu)化已累計服務(wù)超過(guò)900家企業(yè)!     咨詢(xún)熱線(xiàn):13619273150

15年網(wǎng)站建設優(yōu)化經(jīng)驗,900家客戶(hù)的選擇,值得信賴(lài)!


MySQL數據庫:SQL語(yǔ)句不可省去的引號

作者:佚名      發(fā)布時(shí)間:2011-12-21      瀏覽:1912

62.9K


一次在代碼中發(fā)現了一個(gè)BUG,在排查過(guò)程中,差點(diǎn)暈翻;作一個(gè)DBA也碰到這種低級錯誤;大為自嘲。其實(shí)這個(gè)問(wèn)題在官方手冊里就寫(xiě)得很明白;(可見(jiàn)官方文檔的重要性)
mysql>createtableh1 (idint, col1char(1));
Query OK,
0rows affected (0.00sec)

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

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

mysql
>insertintoh1select2,’0′;
Query OK,
1row affected (0.00sec)
Records:
1 Duplicates:0 Warnings:0

mysql>select*fromh1 ;
+——+——+
|id |col1|
+——+——+
| 1|I |
| 2|F |
| 2|0 |
+——+——+
3rowsinset(0.00sec)

mysql
>select*fromh1wherecol1=0;
+——+——+
|id |col1|
+——+——+
| 1|I |
| 2|F |
| 2|0 |
+——+——+
3rowsinset,2warnings (0.00sec)

mysql
>select*fromh1wherecol1=0′;
+——+——+
|id |col1|
+——+——+
| 2|0 |
+——+——+
1rowinset(0.00sec)

mysql>select*fromh1wherecol1=1′;
Empty
set(0.00sec)

mysql
>select*fromh1wherecol1=1;
Empty
set,2warnings (0.00sec)

發(fā)現沒(méi)有:

col1=0 ; 所有行被選中;

col1=’0′ ; 結果集正常

mysql>select*fromh1wherecol1=1′;
Empty
set(0.00sec)

mysql
>select*fromh1wherecol1=1;
Empty
set,2warnings (0.00sec)

想知道為么嗎?

看這里.

mysql>show warnings;
+———+——+—————————————+
|Level |Code|Message |
+———+——+—————————————+
|Warning|1292|Truncated incorrectDOUBLEvalue: ‘I’|
|Warning|1292|Truncated incorrectDOUBLEvalue: ‘F’|
+———+——+—————————————+
2rowsinset(0.00sec)

MySQL當數據類(lèi)型不匹配時(shí),尊重用戶(hù)的輸入,將字段的類(lèi)型做了隱式轉換然后來(lái)匹配,而字符向數字轉,結果都是0;

via IT168技術(shù)

以上內容由寶雞網(wǎng)站建設公司、寶雞網(wǎng)絡(luò )公司---寶雞世紀網(wǎng)絡(luò )公司為您轉摘!我公司是寶雞地區專(zhuān)業(yè)的網(wǎng)站建設服務(wù)提供商,五年建站歷程、六百家客戶(hù)的選擇,值得信賴(lài)!咨詢(xún)熱線(xiàn)0917-3535180 3536690



0