標籤:mysql 字串 int
在MySQL中,在整型欄位中查詢字串,返回了所有為0的結果,應該返回null或空。
例如表結構資訊:
CREATE TABLE `t` ( `name` char(20) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `id` int(11) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
表中資料:
mysql> select id from t;+----+| id |+----+| 0 || 0 || 1 |+----+
查詢字串a,返回了所有是0 的:
mysql> select id from t where id=‘a‘;+----+| id |+----+| 0 || 0 |+----+
警告資訊:
mysql> show warnings;+---------+------+---------------------------------------+| Level | Code | Message |+---------+------+---------------------------------------+| Warning | 1292 | Truncated incorrect DOUBLE value: ‘a‘ |+---------+------+---------------------------------------+
查詢字串‘1a‘,可以返回1:
mysql> select id from t where id=‘1a‘;+----+| id |+----+| 1 |+----+
警示資訊:
mysql> show warnings;+---------+------+----------------------------------------+| Level | Code | Message |+---------+------+----------------------------------------+| Warning | 1292 | Truncated incorrect DOUBLE value: ‘1a‘ |+---------+------+----------------------------------------+
這種情況不經常遇到,解決辦法是在程式中進行資料類型的判斷,對前端傳入的值進行校正
歡迎批評指正
本文出自 “Amnesiasun” 部落格,請務必保留此出處http://amnesiasun.blog.51cto.com/10965283/1931639
MySQL 整型欄位中查詢字串,返回了所有為0的結果