【发布时间】: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");
我想确认这个查询是否正确,或者有什么更好的方法可以简单地重写这个查询。
任何帮助家伙
【问题讨论】:
-
警告:如果您只是学习 PHP,请不要学习过时的
mysql_query接口。这很糟糕,已在 PHP 7 中删除。PDO is not hard to learn 之类的替代品和PHP The Right Way 之类的指南有助于解释最佳实践。确保确保您的用户参数是properly escaped,否则您最终会得到严重的SQL injection bugs。 -
你可以使用
OR,所以你不需要UNION。