MySQL查询给出错误的数据

时间:2018-08-30 09:42:47

标签: php mysqli

select * from stock where product_name like '".$searchType."%' or product_brand like '".$searchType."%' or type like '".$searchType."%' and (price between ".$min_price." and ".$max_price.") group by product_name

我有一个名称为stock的表,其中有列product_nameproduct_brandtypeprice。现在,我有了一个价格范围滑块,该滑块仅适用于type,而不适用于product_nameproduct_brand。例如。

当我滑动价格滑块并将其范围最大为200到500并选择type时,如果type不在范围内,它将显示结果,它将显示result not found。但是对于product_nameproduct_brand,无论价格是否处于价格范围内,它始终会显示数据。

那么,如何解决此问题?请帮助我。

向你致敬

2 个答案:

答案 0 :(得分:2)

您将需要在SELECT中对OR条件进行分组,否则一旦匹配,它将停止检查其他条件...

select * 
  from stock where ( product_name like '".$searchType."%' or 
      product_brand like '".$searchType."%' or 
      type like '".$searchType."%') and 
     (price between ".$min_price." and ".$max_price.") 
  group by product_name

您还应该研究准备好的语句,因为这可以解决其他各种问题(包括SQL注入和搜索词中的流氓引号)。

答案 1 :(得分:-3)

您创建的查询具有很多循环。这就是为什么结果超载的原因。我无法提供确切的解决方案,因为数据库不在我身边。但是,请尝试使用左连接并减少循环。