2012-05-07 33 views
2
--------------------------------------------------------------------------------------- 
| products_id | net_price | special_price | special_expire | special_status 
| ------------------------------------------------------------------------------------- 
| 1   |  500  |  200   | 2012-5-03 00:00 |  1 
| ------------------------------------------------------------------------------------- 
| 2   |  600  |  300   | 2012-6-04 00:00 |  0 
| ------------------------------------------------------------------------------------- 
| 3   |  700  |  400   | 2012-7-05 00:00 |  1 
--------------------------------------------------------------------------------------- 
​​が、 net_pricespecial_priceは、 special_expireは自明である

special_status1を定義します。条件付き選択値

私には価格帯がある検索フォームがあります。

<form method="get" action="http://website.com"> 
    <input type="text" id="ref-search" name="search" value="<?php echo $search_value;?>" class="text" size="55" placeholder="Type a keyword." style="height: 30px; 
    width: 89%;"> 

<label>Price Range:</label><br> 
<span class="price-range"> 
From <input type="text" id="price1" name="pricefrom" 
    value="<?php echo $pricefrom_value; ?>" class="text" style=" 
    height: 20px; 
    width: 22%;"> 

to <input type="text" id="price2" name="priceto" 
    value="<?php echo $priceto_value; ?>" class="text" style=" 
    height: 20px; 
    width: 22%;"> 
</form> 

私はどちらかspecial_price0.000であれば、条件付きでnet_priceを選択するためのソリューションを開発するか、見つけるのに苦労してる、special_expire日付値が経過したかspecial_statusの値は0です。その後、special_priceに値がある場合は、special_expireの日付値が渡されず、special_statusの値が1である場合、special_priceの値を選択します。

これは私が欲しい画像です。 (私は、私は、これは私の問題を描くための最良の方法だと思い、これが不可能であることを知っているが。)

$sql = "select products_id, products_price, special_price, special_expire, 
      special_status, products_name from " . TABLE_GLOBAL_PRODUCTS . " where 
      products_name like '%" . $search_key . "%' 

    and /**Where the condition starts**/ 

    if(special_value is =="0.000" || special_expire == <EXPIRED> || 
     special_status =="0") { 
     products_price >= ".$pricefrom_key." and products_price <= ".$priceto_key." 
    } else { 
     specials_new_products_price >= ".$pricefrom_key." 
     and specials_new_products_price <= ".$priceto_key." order by products_name 
    }"; 

私は ありがとう..あなたが私に人を助けることができると思います。

答えて

3

はこれを試してみてください:

$sql = "SELECT products_id, products_price, special_price, special_expire, 
special_status, products_name, 
case when (special_price > 0 and special_expire > Now() and special_status != 0) 
then special_price else net_price end price from " . TABLE_GLOBAL_PRODUCTS . " 
where products_name like '%" . $search_key . "%' 
having price >= ".$pricefrom_key." and price <= ".$priceto_key."; 

、あなたの入力を逃れることを確認してください!

+0

魅力的な作品です!ありがとうございます!いいぞ! – Ken