-2

私はこれらの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             | 
 -------------------------------------------------------------------

値が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";

これに対する解決策を考えることはできません。助けてください。

4

1 に答える 1

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";
于 2012-05-07T17:26:44.490 に答える