欢迎访问世纪网络官网,宝鸡网站建设SEO优化已累计服务超过900家企业!     咨询热线:13619273150

15年网站建设优化经验,900家客户的选择,值得信赖!


MySQL数据库:SQL语句不可省去的引号

作者:佚名      发布时间:2011-12-21      浏览:1812

62.9K


一次在代码中发现了一个BUG,在排查过程中,差点晕翻;作一个DBA也碰到这种低级错误;大为自嘲。其实这个问题在官方手册里就写得很明白;(可见官方文档的重要性)
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)

发现没有:

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当数据类型不匹配时,尊重用户的输入,将字段的类型做了隐式转换然后来匹配,而字符向数字转,结果都是0;

via IT168技术

以上内容由宝鸡网站建设公司宝鸡网络公司---宝鸡世纪网络公司为您转摘!我公司是宝鸡地区专业的网站建设服务提供商,五年建站历程、六百家客户的选择,值得信赖!咨询热线0917-3535180 3536690



0