【问题标题】:Price range query returns inaccurate results价格范围查询返回不准确的结果
【发布时间】:2012-06-20 12:41:15
【问题描述】:

我有一个搜索表单,用户可以在其中搜索指定价格范围内的产品。 其中我有2个输入框,1个用于起始范围,另一个用于结束范围。

我的数据库表中有 3 个产品商店。价格分别为2300.00001200.00001000.5000

在输入的价格范围内选择值时遇到问题。问题是,当我输入像

这样的值时
  1. 100 起始范围到 1100 结束范围。 1200.0000的产品 并且 1000.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

似乎有什么问题??

【问题讨论】:

  • 数据库中的价格字段定义为什么?是数字还是文字?看起来您的搜索是按字母顺序而不是按数字搜索....
  • 价格是什么字段类型?
  • 请问您为什么使用 HAVING 子句而不是在 WHERE 子句中添加价格信息?
  • @andrewsi 我猜是因为我使用,case when (p.specials_new_products_price &gt; 0 or p.specials_new_products_price != 0000-00-00 and p.expires_date &gt; Now() and p.status != 0) then p.specials_new_products_price else p.products_price end price 有人告诉我使用having clause
  • 这很公平;这不是我经常使用的东西 - 文档中有一条关于仅在聚合子句中使用它的评论,我不知道 case 是否算数......可能值得将它撞到 WHERE 子句中看看它是否会改变结果?

标签: php mysql


【解决方案1】:

我找到了问题..它在案例中..我已将其修改为case when p.specials_new_products_price &gt;= 0.0000 and p.expires_date &gt; Now() and p.status != 0 then p.specials_new_products_price else p.products_price end price

【讨论】:

    【解决方案2】:

    你到底在用这条线做什么..

    $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,它们将被忽略。我希望这是有道理的。

    【讨论】:

    • 他正在尝试构建 SQL 查询,而不是创建输出。
    • 没关系。他的价格范围规格仍然是错误的。我的例子只是为了指出正确的方向。如果他需要 SQL 查询,他应该要求它。看我上面的编辑.....
    猜你喜欢
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-06
    • 1970-01-01
    • 2019-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多