奇怪!日期类型与任意字符串比较都能成功执行.

MySql 码拜 10年前 (2014-04-27) 1395次浏览 0个评论

table schema:


create table if not exists `RIV_INBOUND_NOTICE_H`(
	`INH_ID` int(10) primary key auto_increment comment ""入库通知单头ID"",
	`INH_STATUS` smallint not null comment ""状态;10-已输入,20-已确认,90-已作废"",
	`INH_NOTICE_NO` varchar(40) not null comment ""入库通知单号"",
	`INH_OWNER_ID` int(10) not null comment ""货主ID"",
	`INH_ORG_ID` int(10) not null comment ""机构ID"",
	`INH_OWNER_NO` varchar(40)  comment ""货主单号"",
	`INH_TX_TYPE_ID` int(10) not null comment ""入库事务类型ID;对应事务类型表"",
	`INH_VENDOR_ID` int(10) comment ""供应商ID;对应货主关联往来户表"",
	`INH_EXPECT_DATETIME` datetime comment ""预计入库日期时间"", 
	`INH_TOTAL_GROSS_WEIGHT` decimal(18,6) not null default 0 comment ""总毛重"",
	`INH_TOTAL_NET_WEIGHT` decimal(18,6) not null default 0 comment ""总净重"",
	`INH_TOTAL_CUBAGE` decimal(18,6) not null default 0 comment ""总体积"",
	`INH_TOTAL_PACKS` int(10) not null default 0 comment ""总件数"",
	`INH_TOTAL_MONEY` decimal(18,6) not null default 0 comment ""总金额"",
	`INH_INPUT_DATETIME` datetime not null comment ""下单日期时间"",
	`INH_INPUT_USER_ID` int(10) not null comment  ""下单操作员ID"",
	`INH_APPROVE_DATETIME` datetime comment ""确认日期时间"",
	`INH_APPROVE_USER_ID` int(10) comment ""确认用户ID"",
	`INH_FINISH_DATETIME` datetime comment ""完成日期时间"",
	`INH_FINISH_USER_ID` int(10) comment ""完成用户ID"",
	`INH_REMARK` varchar(400) comment ""备注"",
	`GEN_METHOD` varchar(40) not null comment ""资料生成方式:input - 手工输入,system - 系统自动生成,file - 从文件导入,interface - 从接口导入"", 
	`CREATE_USER_ID` int(10) not null comment ""创建用户ID"",
	`CREATE_TIME` datetime not null comment ""创建时间"",
	`UPDATE_USER_ID` int(10) comment ""修改用户ID"",
	`UPDATE_TIME` timestamp comment ""修改时间"",
	`VERSION_NUMBER` int(10) not null default 0 comment ""版本号:新增资料为 0,每次修改加 1""
) engine=innodb default charset=utf8 comment ""入库通知单头表"";

query :

SELECT
create_time,
create_user_id,
gen_method,
inh_approve_datetime,
inh_approve_user_id,
inh_expect_datetime,
inh_finish_datetime,
inh_finish_user_id,
inh_id,
inh_input_datetime,
inh_input_user_id,
inh_notice_no,
inh_org_id,
inh_owner_id,
inh_owner_no,
inh_remark,
inh_status,
inh_total_cubage,
inh_total_gross_weight,
inh_total_money,
inh_total_net_weight,
inh_total_packs,
inh_tx_type_id,
inh_vendor_id,
update_time,
update_user_id,
version_number
FROM
riv_inbound_notice_h
WHERE inh_notice_no = ""ASN20150220000002""
AND inh_input_datetime >= ""-  -""
AND inh_input_datetime <= ""-  -""
AND inh_expect_datetime >= ""-  -""
AND inh_expect_datetime <= ""-  -""
ORDER BY
update_time DESC
15分
MySQL 的文档 (Type Conversion in Expression Evaluation) 中提到,在做比较时,会按这样的规则进行必要的类型转换:

两个参数至少有一个是 NULL 时,比较的结果也是 NULL,例外是使用 <=> 对两个 NULL 做比较时会返回 1,这两种情况都不需要做类型转换
两个参数都是字符串,会按照字符串来比较,不做类型转换
两个参数都是整数,按照整数来比较,不做类型转换
十六进制的值和非数字做比较时,会被当做二进制串,和数字做比较时会按下面的规则处理
有一个参数是 TIMESTAMP 或 DATETIME,并且另外一个参数是常量,常量会被转换为 timestamp
有一个参数是 decimal 类型,如果另外一个参数是 decimal 或者整数,会将整数转换为 decimal 后进行比较,如果另外一个参数是浮点数,则会把 decimal 转换为浮点数进行比较
所有其他情况下,两个参数都会被转换为浮点数再进行比较

10分
show warnings;看告警

mysql> show warnings;
+———+——+—————————————————————————-+
| Level   | Code | Message                                                                    |
+———+——+—————————————————————————-+
| Warning | 1292 | Incorrect datetime value: “”-  -“” for column “”inh_input_datetime”” at row 1  |
| Warning | 1292 | Incorrect datetime value: “”-  -“” for column “”inh_input_datetime”” at row 1  |
| Warning | 1292 | Incorrect datetime value: “”-  -“” for column “”inh_expect_datetime”” at row 1 |
| Warning | 1292 | Incorrect datetime value: “”-  -“” for column “”inh_expect_datetime”” at row 1 |
+———+——+—————————————————————————-+
4 rows in set (0.82 sec)
这种不合法的日期格式会被当做什么值去处理,这个在文档、或者代码上有没有说明

15分
引用 3 楼 u010211377 的回复:

mysql> show warnings;
+———+——+—————————————————————————-+
| Level   | Code | Message                                                                    |
+———+——+—————————————————————————-+
| Warning | 1292 | Incorrect datetime value: “”-  -“” for column “”inh_input_datetime”” at row 1  |
| Warning | 1292 | Incorrect datetime value: “”-  -“” for column “”inh_input_datetime”” at row 1  |
| Warning | 1292 | Incorrect datetime value: “”-  -“” for column “”inh_expect_datetime”” at row 1 |
| Warning | 1292 | Incorrect datetime value: “”-  -“” for column “”inh_expect_datetime”” at row 1 |
+———+——+—————————————————————————-+
4 rows in set (0.82 sec)
这种不合法的日期格式会被当做什么值去处理,这个在文档、或者代码上有没有说明

MYSQL的源代码肯定有的。
除非自己下载MYSQL源代码分析外,其实可以做个实现。 当 datatime 与 varchar 比较时会转换成什么。

结论是转换成 char

引用 1 楼 rucypli 的回复:

MySQL 的文档 (Type Conversion in Expression Evaluation) 中提到,在做比较时,会按这样的规则进行必要的类型转换:

两个参数至少有一个是 NULL 时,比较的结果也是 NULL,例外是使用 <=> 对两个 NULL 做比较时会返回 1,这两种情况都不需要做类型转换
两个参数都是字符串,会按照字符串来比较,不做类型转换
两个参数都是整数,按照整数来比较,不做类型转换
十六进制的值和非数字做比较时,会被当做二进制串,和数字做比较时会按下面的规则处理
有一个参数是 TIMESTAMP 或 DATETIME,并且另外一个参数是常量,常量会被转换为 timestamp
有一个参数是 decimal 类型,如果另外一个参数是 decimal 或者整数,会将整数转换为 decimal 后进行比较,如果另外一个参数是浮点数,则会把 decimal 转换为浮点数进行比较
所有其他情况下,两个参数都会被转换为浮点数再进行比较

引用 4 楼 ACMAIN_CHM 的回复:
Quote: 引用 3 楼 u010211377 的回复:

mysql> show warnings;
+———+——+—————————————————————————-+
| Level   | Code | Message                                                                    |
+———+——+—————————————————————————-+
| Warning | 1292 | Incorrect datetime value: “”-  -“” for column “”inh_input_datetime”” at row 1  |
| Warning | 1292 | Incorrect datetime value: “”-  -“” for column “”inh_input_datetime”” at row 1  |
| Warning | 1292 | Incorrect datetime value: “”-  -“” for column “”inh_expect_datetime”” at row 1 |
| Warning | 1292 | Incorrect datetime value: “”-  -“” for column “”inh_expect_datetime”” at row 1 |
+———+——+—————————————————————————-+
4 rows in set (0.82 sec)
这种不合法的日期格式会被当做什么值去处理,这个在文档、或者代码上有没有说明

MYSQL的源代码肯定有的。
除非自己下载MYSQL源代码分析外,其实可以做个实现。 当 datatime 与 varchar 比较时会转换成什么。

结论是转换成 char

mysql> select convert(“”-  -“”,char);
+———————-+
| convert(“”-  -“”,char) |
+———————-+
| –  –                 |
+———————-+
1 row in set (0.77 sec)

mysql> select convert(“”-  -“”,datetime);
+————————–+
| convert(“”-  -“”,datetime) |
+————————–+
| NULL                     |
+————————–+
1 row in set, 1 warning (0.14 sec)

mysql> show warnings;
+———+——+———————————-+
| Level   | Code | Message                          |
+———+——+———————————-+
| Warning | 1292 | Incorrect datetime value: “”-  -“” |
+———+——+———————————-+
1 row in set (0.02 sec)

可不可以这么理解:

SELECT
create_time,
create_user_id,
gen_method,
inh_approve_datetime,
inh_approve_user_id,
inh_expect_datetime,
inh_finish_datetime,
inh_finish_user_id,
inh_id,
inh_input_datetime,
inh_input_user_id,
inh_notice_no,
inh_org_id,
inh_owner_id,
inh_owner_no,
inh_remark,
inh_status,
inh_total_cubage,
inh_total_gross_weight,
inh_total_money,
inh_total_net_weight,
inh_total_packs,
inh_tx_type_id,
inh_vendor_id,
update_time,
update_user_id,
version_number
FROM
riv_inbound_notice_h
WHERE inh_notice_no = ""ASN20150220000002""
AND inh_input_datetime >= null
AND inh_input_datetime <= null
AND inh_expect_datetime >= null
AND inh_expect_datetime <= null 
ORDER BY
update_time DESC
引用 4 楼 ACMAIN_CHM 的回复:
Quote: 引用 3 楼 u010211377 的回复:

mysql> show warnings;
+———+——+—————————————————————————-+
| Level   | Code | Message                                                                    |
+———+——+—————————————————————————-+
| Warning | 1292 | Incorrect datetime value: “”-  -“” for column “”inh_input_datetime”” at row 1  |
| Warning | 1292 | Incorrect datetime value: “”-  -“” for column “”inh_input_datetime”” at row 1  |
| Warning | 1292 | Incorrect datetime value: “”-  -“” for column “”inh_expect_datetime”” at row 1 |
| Warning | 1292 | Incorrect datetime value: “”-  -“” for column “”inh_expect_datetime”” at row 1 |
+———+——+—————————————————————————-+
4 rows in set (0.82 sec)
这种不合法的日期格式会被当做什么值去处理,这个在文档、或者代码上有没有说明

MYSQL的源代码肯定有的。
除非自己下载MYSQL源代码分析外,其实可以做个实现。 当 datatime 与 varchar 比较时会转换成什么。

结论是转换成 char

这个实验怎么做呢?求指导…


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明奇怪!日期类型与任意字符串比较都能成功执行.
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!