一个mysql中强制转化的现象

来源:互联网 发布:人民币汇率波动率数据 编辑:程序博客网 时间:2024/06/04 20:02



现象如下:
mysql> select servicetree_id,server_id from  servicetree_server where  servicetree_id in ('1584','notreeid') ;
+----------------+-----------+
| servicetree_id | server_id |
+----------------+-----------+
|              0 |      1153 | 
|              0 |      1170 | 
|           1584 |      9008 | 
|           1584 |      9011 | 
|           1584 |     19339 | 
|           1584 |     20468 | 
|           1584 |     20459 | 
|           1584 |     20457 | 
|           1584 |     20458 | 
|           1584 |     20460 | 
|           1584 |     20465 | 
|           1584 |     20463 | 
|           1584 |     20466 | 
|           1584 |      5033 | 
|           1584 |      1183 | 
|           1584 |      5034 | 
|           1584 |      5035 | 
|           1584 |      5036 | 
|           1584 |      9010 | 
+----------------+-----------+
19 rows in set, 1 warning (0.02 sec)
 
mysql> select servicetree_id,server_id from  servicetree_server where  servicetree_id in ('1584','-1') ;
+----------------+-----------+
| servicetree_id | server_id |
+----------------+-----------+
|           1584 |      9008 | 
|           1584 |      9011 | 
|           1584 |     19339 | 
|           1584 |     20468 | 
|           1584 |     20459 | 
|           1584 |     20457 | 
|           1584 |     20458 | 
|           1584 |     20460 | 
|           1584 |     20465 | 
|           1584 |     20463 | 
|           1584 |     20466 | 
|           1584 |      5033 | 
|           1584 |      1183 | 
|           1584 |      5034 | 
|           1584 |      5035 | 
|           1584 |      5036 | 
|           1584 |      9010 | 
+----------------+-----------+
17 rows in set (0.00 sec)
原因:
servicetree_id字段类型为int
字符串"notreeid"被转成0
 建议:
不要自己使用字符串拼凑 mysql搜索中的 in 字符串,
尽量使用php中的implode函数组装,则不必附加此类的字符串
使用implode也应该注意一下当implode的数组为空的情况下,会导致查询失败,直接返回null,所以要对implode的数组先进行一下空判断