簡體   English   中英

價格范圍查詢返回的結果不正確

[英]Price range query returns inaccurate results

我有一個搜索表,用戶可以在其指定價格范圍內搜索產品。 我有2個輸入框,其中1個是開始范圍,另一個是結束范圍。

我的數據庫表中有3個產品商店。 隨着價格2300.00001200.00001000.5000分別。

我在輸入的價格范圍內選擇值時遇到問題。 問題是,當我輸入類似

  1. 100開始范圍至1100結束范圍。 與產品1200.00001000.5000正在顯示的,而不是只有1000.5000產品。
  2. 0開始范圍至1100結束范圍或1000.50結束范圍。 與產品1200.00001000.5000正在顯示的,而不是只有1000.5000產品。
  3. 1100開始范圍。 只有產品2300.0000被顯示,而不是兩個2300.00001200.0000 但是,當起始值小於或等於500 ,將顯示所有3個產品。

這是我正在使用的代碼。

function db_prepare_input($string)
{
    if (is_string($string)) {
        return trim(stripslashes($string));
    } elseif (is_array($string)) {
        reset($string);
        while (list($key, $value) = each($string)) {
            $string[$key] = db_prepare_input($value);
        }
        return mysql_real_escape_string($string);
    } else {
        return mysql_real_escape_string($string);
    }
}

$pricefrom_key = db_prepare_input(number_format($_GET['pricefrom'], 4, '.', ''));
$priceto_key = db_prepare_input(number_format($_GET['priceto'], 4, '.', ''));

// Price Range
if (!empty($_GET['pricefrom']) && !empty($_GET['priceto'])) {
    $price_range = " having price >= ".$pricefrom_key." and price <= ".$priceto_key;
}
elseif(empty($_GET['pricefrom']) && !empty($_GET['priceto']))
{
    $price_range = " having price >= 0 and price <= ".$priceto_key;
}

elseif(!empty($_GET['pricefrom']) && empty($_GET['priceto']))
{
    $price_range = " having price >= ".$pricefrom_key;
}

elseif(!empty($_GET['pricefrom']) && !empty($_GET['priceto']))
{
    $price_range = "";
}

$catglobal_sql = "select p.blog_id, p.global_category_id, p.products_id, p.products_currency, p.products_type, p.products_name, p.products_description, p.products_quantity, p.products_model, p.products_image, p.products_price, p.products_date_added, p.products_last_modified, p.products_date_available, p.products_weight, p.products_status, p.products_tax_class_id, p.manufacturers_id, p.products_ordered, p.specials_new_products_price, p.specials_date_added, p.specials_last_modified, p.expires_date, p.date_status_change, p.status, p.display_product, case when (p.specials_new_products_price > 0 or p.specials_new_products_price != 0000-00-00 and p.expires_date > Now() and p.status != 0) then p.specials_new_products_price else p.products_price end price from ".TABLE_GLOBAL_PRODUCTS." p INNER JOIN ".TABLE_STORES." s ON s.blog_id = p.blog_id where MATCH (p.products_name,p.products_description) AGAINST ('%".$search_key."%') OR p.products_name like '%".$search_key."%' and s.countries_id = '168' ".$search_cat." and p.display_product = '1' and p.products_status = '1' ".$price_range." order by p.products_date_added DESC, p.products_name";

我嘗試過添加die($catglobal_sql); 結果是

select p.blog_id, p.global_category_id, p.products_id, p.products_currency, p.products_type, p.products_name, p.products_description, p.products_quantity, p.products_model, p.products_image, p.products_price, p.products_date_added, p.products_last_modified, p.products_date_available, p.products_weight, p.products_status, p.products_tax_class_id, p.manufacturers_id, p.products_ordered, p.specials_new_products_price, p.specials_date_added, p.specials_last_modified, p.expires_date, p.date_status_change, p.status, p.display_product, case when (p.specials_new_products_price > 0 or p.specials_new_products_price != 0000-00-00 and p.expires_date > Now() and p.status != 0) then p.specials_new_products_price else p.products_price end price from wp_global_products_table p INNER JOIN wp_blogs s ON s.blog_id = p.blog_id where MATCH (p.products_name,p.products_description) AGAINST ('%cotton%') OR p.products_name like '%cotton%' and s.countries_id = '168' and p.products_currency = 'php' and p.display_product = '1' and p.products_status = '1' and p.products_type = 'a' having price >= 0 and price <= 1200.0000 order by p.products_date_added DESC, p.products_name

似乎是什么問題?

我發現了問題.. case when p.specials_new_products_price >= 0.0000 and p.expires_date > Now() and p.status != 0 then p.specials_new_products_price else p.products_price end price而言..我將其修改為case when p.specials_new_products_price >= 0.0000 and p.expires_date > Now() and p.status != 0 then p.specials_new_products_price else p.products_price end price

您到底在用這條線做什么?

$price_range = " having price >= ".$pricefrom_key." and price <= ".$priceto_key;

您不應該只設置$ price_range,即:

if (!empty($_GET['pricefrom']) && !empty($_GET['priceto']))

{

      if($price_range >= $pricefrom_key && $price_range <= $priceto_key)
      {
      echo "you are in the pricerange from" .$pricefrom_key." to ".$priceto_key;
      } 
//etc......
}

編輯:由於有些人沒有得到我的答案,所以這只是指向正確的方向。 我們使用“ echo”語句還是SQL查詢都沒有關系,您仍然需要正確指定范圍。 該聲明:

$price_range = " having price >= ".$pricefrom_key." and price <= ".$priceto_key;

..幾乎說:價格范圍=一些文字。 號。 一些文字。 數字<=>運算符位於“”中,表示TEXT,它們將被忽略。 我希望這是有道理的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM