MySQL 整型欄位中查詢字串,返回了所有為0的結果

來源:互聯網
上載者:User

標籤: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的結果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.