简体   繁体   English

为什么使用 Mysql 查询结果不同

[英]Why Different Results Using Mysql Queries

I have two queries.我有两个疑问。 No error in these queries just results are different.这些查询中没有错误,只是结果不同。

Query 1查询 1

SELECT * FROM notices
WHERE CURDATE( ) < end_date
OR end_date = NULL
ORDER BY id

Result of Query One:查询结果一:

在此处输入图片说明

Query 2查询 2

select * from notices
where CURDATE() < end_date or end_date='0000-00-00' 
order by id

Result of Query Two:查询二结果:

在此处输入图片说明

Table Structure表结构

在此处输入图片说明

Question

My Question is that why query 1 not showing the same results as query 2 showing?我的问题是为什么查询 1 显示的结果与查询 2 显示的结果不同?

Because NULL is not the same as "0000-00-00"因为 NULL 与“0000-00-00”不同

PD: If you want to set a where clause and select where field is null you should use "WHERE end_date IS NULL", not "WHERE end_date = NULL" PD:如果要设置 where 子句并选择 where 字段为 null,则应使用“WHERE end_date IS NULL”,而不是“WHERE end_date = NULL”

From my understanding NULL is different from 0000-00-00 for Date type.Some developer use 0000-00-00 as a default value.根据我的理解,日期类型的 NULL 与 0000-00-00 不同。一些开发人员使用 0000-00-00 作为默认值。 Since both NULL and 0000-00-00 are different, you are seeing two different resultsets.由于 NULL 和 0000-00-00 是不同的,您会看到两个不同的结果集。

Check this default date '0000-00-00 00:00:00' or null检查这个默认日期 '0000-00-00 00:00:00' 或 null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM