我想根据今天的日期选择具有开始日期和到期日期列的行,甚至没有日期值的行。所以
$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");
我想确认这个查询是否正确,或者是否有更好的方法来简单地重写这个查询。
大家有什么帮助
最佳答案
使用此查询:
$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");
关于Mysql 查询选择带或不带日期的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43601417/