我有这些mysql表,
-------------------------------------------------------------------
| products_id | date_added | date_modified |
| -----------------------------------------------------------------
| 1 | 2012-03-31 15:36:01 | 2012-5-03 00:00 |
| -----------------------------------------------------------------
| 2 | 2012-03-31 15:38:01 | 2012-5-04 00:00 |
| -----------------------------------------------------------------
| 3 | 2012-03-31 15:40:01 | NULL |
-------------------------------------------------------------------
我希望显示值,具体取决于值不为空的所选时间。
如果date_modified具有日期值,则使用date_modified作为其日期。如果它为NULL,则使用date_added作为其日期。
if($_get['duration'] =="0") {
$duration = <select all regardless of date>;
}
elseif ($_get['duration'] =="24") {
$duration = <select all that less than 24 hours>;
}
elseif ($_get['duration'] =="15") {
$duration = <select all that less than 15 days>;
}
这是我的mysql查询,
$catglobal_sql = "select blog_id, global_category_id, products_id, products_currency, products_type, products_name, products_description, products_quantity, products_model, products_image, products_price, products_date_added, products_last_modified, products_date_available, products_weight, products_status, products_tax_class_id, manufacturers_id, products_ordered, specials_new_products_price, specials_date_added, specials_last_modified, expires_date, date_status_change, status, display_product, case when (specials_new_products_price > 0 and expires_date > Now() and status != 0) then specials_new_products_price else products_price end price from " . TABLE_GLOBAL_PRODUCTS . " where products_name like '%" . $search_key . "%' and products_currency = '" . $currency_key . "' and display_product = '1' and products_status = '1' having price >= ".$pricefrom_key." and price <= ".$priceto_key." order by products_date_added DESC, products_name";
无法想到解决方案。请帮忙。
答案 0 :(得分:0)
假设您要使用expires_date
日期,且类型为date
或datetime
$duration = "";
if ($_get['duration'] == "24") {
$duration = " AND DATE_SUB(expires_date, INTERVAL 24 HOUR) > NOW() ";
}
elseif ($_get['duration'] == "15") {
$duration = " AND DATE_SUB(expires_date, INTERVAL 15 DAY) > NOW() ";
}
$catglobal_sql = "select
<your_fields>
FROM " . TABLE_GLOBAL_PRODUCTS . "
WHERE
products_name like '%" . $search_key . "%' and
products_currency = '" . $currency_key . "' and
display_product = '1' and
products_status = '1'
$duration
HAVING price >= ".$pricefrom_key." and price <= ".$priceto_key."
ORDER BY products_date_added DESC, products_name";