为什么 MySQL 中比较类型不会引发错误?
如果你尝试比较字符串和 int,MySQL 不会引发错误,因为它会将字符串转换为 int。让我们先创建一个表 -
mysql> create table DemoTable1852 ( Value1 varchar(20), Value2 int ); Query OK, 0 rows affected (0.00 sec)
使用 insert 命令在表中插入一些记录 -
mysql> insert into DemoTable1852 values('1John',1);
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1852 values('John',1);
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1852 values('1',1);
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1852 values('John1',1);
Query OK, 1 row affected (0.00 sec)使用 select 语句显示表中的所有记录 -
mysql> select * from DemoTable1852;
这将生成以下输出 -
+--------+--------+ | Value1 | Value2 | +--------+--------+ | 1John | 1 | | John | 1 | | 1 | 1 | | John1 | 1 | +--------+--------+ 4 rows in set (0.00 sec)
以下是在 MySQL 中比较类型并且不会引发错误的查询 -
mysql> select Value1,Value2, Value1=Value2 as Result from DemoTable1852;
这将生成以下输出 -
+--------+--------+--------+ | Value1 | Value2 | Result | +--------+--------+--------+ | 1John | 1 | 1 | | John | 1 | 0 | | 1 | 1 | 1 | | John1 | 1 | 0 | +--------+--------+--------+ 4 rows in set, 3 warnings (0.00 sec)
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP