选择空的MySQL日期时间字段 [英] Selecting empty mysql datetime fields

查看:192
本文介绍了选择空的MySQL日期时间字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有比这更好的选择空日期时间字段的方法了?

Is there a better way to select empty datetime fields than this?

SELECT * FROM `table` WHERE `datetime_field` = '0000-00-00 00:00:00'

推荐答案

哪种方式更好?该查询会执行您所要求的所有操作,并且只要在datetime_field上有一个索引,它就会像它将要获得的一样快.

Better in what way? That query does everything you ask of it and, provided there's an index on datetime_field, it's as fast as it's going to get.

如果您担心查询看起来丑陋",那就不要.其意图很明确.

If you're worried about the query looking "ugly", don't be. Its intent is quite clear.

您可以考虑的唯一可能的改进就是对这些零日期/时间行使用NULL,在这种情况下,查询将变为:

The only possible improvement you could consider is to use NULLs for these zero-date/time rows, in which case your query becomes:

SELECT * FROM `table` WHERE `datetime_field` IS NULL

这就是我要使用符合标准的DBMS的方式.我的理解是,MySQL 可能以这种令人恐惧的方式表示真正的NULL datetime字段,在这种情况下,我想IS NULL可能不起作用.

That's what I'd do with a standards-compliant DBMS. My understanding is that MySQL may represent true NULL datetime fields in this horrific manner, in which case I guess the IS NULL may not work.

这篇关于选择空的MySQL日期时间字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆