2012-05-07 62 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_pricespecial_expire是自我解釋和定義special_status作爲1應用特殊的價格和0爲禁用特惠價。對MySQL表有條件選擇值

我有一個搜索表單,其中有一個價格範圍。

<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> 

我有一個很難開發或尋找解決有條件地選擇net_price如果任special_price0.000,則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