MYSQL:比较NULL日期和CURRENT_DATE [英] MYSQL: comparing a NULL date to CURRENT_DATE
问题描述
我有一个带有新闻文章的MYSQL表,其中的新闻文章具有"date_start"和"date_end"字段,以指示要在网站上显示的新闻.如果date_start在今天之前并且date_end还没有过去(今天之后),则文章是公开的.
I have a MYSQL table with news articles with the fields "date_start" and "date_end" to indicate which news to show on a web site. Articles are public if date_start is before today and the date_end hasn't already gone by (after today).
问题: 如果文章具有永久性且不会过期,我想让管理员将date_end保留为NULL.当然,这不适用于我的选择:
The problem: I'd like to let the admin leave date_end NULL if the article is of permanent nature and doesn't expire. This of course doesn't work with my select:
SELECT * FROM pf_news WHERE date_start <= CURRENT_DATE() AND date_end >= CURRENT_DATE()
它省略了带有NULL date_end
的文章.我尝试使用IF语句,但是这让我感到困惑.有没有简单的方法可以执行此操作,还是应该将date_end保留为3000-01-01
(如果它为空)? :)
It leaves out articles with a NULL date_end
. I tried playing a bit with IF statements but it got confusing for me. Is there a simple way of doing this, or should I just set date_end to 3000-01-01
if it's left empty? :)
推荐答案
您可以尝试:
SELECT * FROM pf_news WHERE date_start <= CURRENT_DATE() AND
(date_end >= CURRENT_DATE() OR date_end IS NULL)
或一些类似的逻辑分组.
or some similar logical grouping.
这篇关于MYSQL:比较NULL日期和CURRENT_DATE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!