SQL查询价格比较

时间:2013-10-07 09:03:37

标签: php sql search

我正在运行SQL查询来搜索我的数据库。我现在可以搜索所有内容,直到我比较价格。

没有价格比较的工作代码

$query = "SELECT * FROM propertie_ids WHERE (`search` LIKE '%".$searchquery."%') AND (`location` = '$location') AND (`bathroom` = '$bathroom') AND (`bedroom` = '$bathroom');

价格比较破碎的代码

$query = "SELECT * FROM propertie_ids WHERE (`search` LIKE '%".$searchquery."%') AND (`location` = '$location') AND (`bathroom` = '$bathroom') AND (`bedroom` = '$bathroom') AND price BETWEEN ('0') AND  ('10000000000')";

为什么这不起作用的任何想法,我已经检查并且我的所有表都被正确命名。

3 个答案:

答案 0 :(得分:1)

单引号表示SQL中的字符串,因此使用它们将使您的查询进行字典比较。 如果你删除它们,你将得到你可能想要的数字比较。另外,由于你有price作为字符串,你可能也需要强制转换它。

$query = "SELECT * FROM propertie_ids WHERE (`search` LIKE '%".$searchquery."%') AND (`location` = '$location') AND (`bathroom` = '$bathroom') AND (`bedroom` = '$bathroom') AND CAST (price AS DECIMAL) BETWEEN (0) AND  (10000000000)";

答案 1 :(得分:1)

我认为价格应该是十进制/双/货币。你在这里测试字符串值!

$query = "SELECT * FROM propertie_ids 
WHERE (`search` LIKE '%".$searchquery."%') AND (`location` = '$location') 
AND (`bathroom` = '$bathroom') AND (`bedroom` = '$bathroom') 
AND price BETWEEN 0 AND 10000000000";

如果价格存储在字符串中,您必须转换它,因为您无法在两个字符串之间声明字符串!

$query = "SELECT * FROM propertie_ids 
WHERE (`search` LIKE '%".$searchquery."%') AND (`location` = '$location') 
AND (`bathroom` = '$bathroom') AND (`bedroom` = '$bathroom') 
AND CAST(price as DECIMAL(10,2)) BETWEEN 0 AND 10000000000";

答案 2 :(得分:1)

http://sqlfiddle.com/#!2/abbcb/4/0

我认为价格列应该在INT中,你应该从查询中删除引号,它会起作用。您可以使用price*1

来代替演员
$query = "SELECT * FROM propertie_ids WHERE (`search` LIKE '%".$searchquery."%') AND (`location` = '$location') AND (`bathroom` = '$bathroom') AND (`bedroom` = '$bathroom') AND price BETWEEN 0 AND  10000000000";

`SELECT * FROM test where price*1 between '5' and '13'`

此处介于0和10000000000之间,表示它也包含0和10000000000。 我建议介于1和10000000000之间。