Mysql Query选择有或没有日期的行

时间:2017-04-25 04:09:24

标签: mysql date select

enter image description here

我想根据今天的日期选择行,包括开始日期和到期日期列,甚至是没有日期值的行。所以

$query=mysql_query("SELECT * FROM discounts where '".date('Y-m-d')."' >= coupon_start_date AND '".date('Y-m-d')."' <= coupon_expiry_date
UNION SELECT * FROM ecwid_discounts where '".date('Y-m-d')."' >= coupon_start_date AND coupon_expiry_date is NULL union SELECT * FROM ecwid_discounts where coupon_start_date is NULL AND '".date('Y-m-d')."' <= coupon_expiry_date union SELECT * FROM ecwid_discounts where coupon_start_date is NULL AND coupon_expiry_date is null");

我想确认此查询是否正确,或者是否有更好的方法可以简单地重写此查询。

任何帮助人员

1 个答案:

答案 0 :(得分:0)

使用此查询:

$query=mysql_query("SELECT * FROM discounts where '".date('Y-m-d')."' >= coupon_start_date AND '".date('Y-m-d')."' <= coupon_expiry_date
UNION SELECT * FROM ecwid_discounts where '".date('Y-m-d')."' >= coupon_start_date AND '".date('Y-m-d')."' <= coupon_expiry_date OR '".date('Y-m-d')."' >= coupon_start_date AND coupon_expiry_date is NULL OR coupon_start_date is NULL AND '".date('Y-m-d')."' <= coupon_expiry_date OR coupon_start_date is NULL AND coupon_expiry_date is null");