mysql的意外结果选择在哪里

时间:2016-03-29 05:11:04

标签: mysql

在php应用程序中,我根据其他查询的结果进行了查询。 第一个查询列出了一些行,每个行由一个id标识,第二个查询使用此id来选择结果。

我在第一个查询中使用了一个代码,其中group_concat是id,但是我忘了解决这些ID,而且 - 令我惊讶的是 - 第二个查询有效,选择了这个group_concat结果的第一个id。

它独立于ID之间的分隔符。

这是预期的行为吗?

mysql> select * from parts;
+--------+-------------------------------+---------+
| partid | partname                      | partabr |
+--------+-------------------------------+---------+
|     70 | Ring and pinion               | CP      |
|     71 | Complete differential case    | CDC     |
|     72 | Empty differential case       | CDV     |
|     73 | Differential case repair kit  | KCD     |
|     74 | Shim kit                      | KC      |
|     75 | Differential case bearing kit | KRC     |
|     76 | Pinion gear bearing kit       | KRP     |
|     77 | Axle shaft assembly           | SEC     |
|     78 | Axle shaft                    | SE      |
|     79 | Axle shaft repair kit         | KSE     |
|     80 | Differential end fitting      | NULL    |
|     81 | End yoke                      | TD      |
|     82 | Companion flange              | FAD     |
+--------+-------------------------------+---------+
13 rows in set (0.00 sec)

mysql> select * from parts where partid = '78,72';
+--------+------------+---------+
| partid | partname   | partabr |
+--------+------------+---------+
|     78 | Axle shaft | SE      |
+--------+------------+---------+
1 row in set, 1 warning (0.00 sec)

mysql> select * from parts where partid = '78 # 72';
+--------+------------+---------+
| partid | partname   | partabr |
+--------+------------+---------+
|     78 | Axle shaft | SE      |
+--------+------------+---------+
1 row in set, 1 warning (0.00 sec)

1 个答案:

答案 0 :(得分:2)

正如所料。

原因如下:

(MySQL将尽力将' 78,72'视为整数)

mysql> select '78,72' = 78, '78 # 72' = 78;
+--------------+----------------+
| '78,72' = 78 | '78 # 72' = 78 |
+--------------+----------------+
|            1 |              1 |
+--------------+----------------+
1 row in set, 2 warnings (0.00 sec)

mysql> show warnings;
+---------+------+---------------------------------------------+
| Level   | Code | Message                                     |
+---------+------+---------------------------------------------+
| Warning | 1292 | Truncated incorrect DOUBLE value: '78,72'   |
| Warning | 1292 | Truncated incorrect DOUBLE value: '78 # 72' |
+---------+------+---------------------------------------------+
2 rows in set (0.00 sec)

您可以改为使用FIND_IN_SET

select * from parts where FIND_IN_SET(partid, '78,72');