【问题标题】:Mysql Query to select rows with or without datesMysql 查询以选择有或没有日期的行
【发布时间】:2017-09-21 22:07:15
【问题描述】:

我想选择基于今天日期的行,其中包含开始日期和到期日期列,甚至是没有日期值的行。所以

$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");

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

任何帮助家伙

【问题讨论】:

标签: mysql date select


【解决方案1】:

使用这个查询:

$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");

【讨论】:

    猜你喜欢
    • 2022-01-10
    • 1970-01-01
    • 2012-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-02
    相关资源
    最近更新 更多