1

我正在运行 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')";

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

4

3 回答 3

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)";
于 2013-10-07T09:07:13.450 回答
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";
于 2013-10-07T09:07:38.820 回答
1

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

我认为价格列应该在 INT 中,你应该从查询中删除引号,它会起作用。你可以使用 like 而不是 castprice*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 之间。

于 2013-10-07T09:12:54.720 回答