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

其中products_id, net_price, special_price,special_expire是不言自明的,special_status定义1为应用特价和0禁用特价。

我有一个搜索表单,其中有一个价格范围。

<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.000special_expire日期值已通过或special_status的值是0。然后,如果 中有一个值,则special_price日期special_expire值尚未通过,然后选择该special_status值。1special_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
    }";

我希望你们能帮助我。谢谢。

4

1 回答 1

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.";

确保你逃避你的输入!

于 2012-05-07T02:21:27.343 回答