从mysql中选择多个数组

时间:2012-05-27 01:26:42

标签: php mysql

我正在尝试搜索我正在为朋友工作的房产网站。 在数据库中,属性类型由id编号命名,即:house = 30,flat = 8,terraced = 1,依旧等等。

当一些是价值为2且房屋价值为30等的独立式住宅时,如何从数据库中检索所有属性:) 它让我陷入了困境 这是我到目前为止没有工作的......

$bedrooms = $_GET['bedrooms'];
$pricefrom = $_GET['pricefrom'];
$priceto = $_GET['priceto'];
$proptype = $_GET['proptype'];

if($proptype == 'house'){
$search_propsubid = array('1,2,3,4,5,6,21,22,23,24,26,27,30');
}elseif($proptype == 'flat'){
$search_propsubid = array('7,8,9,10,11,28,29,44');
}elseif($proptype == 'bungalow'){
$search_propsubid = array('');
}


$sql = mysql_query("SELECT * FROM `properties` WHERE `PROP_SUB_ID`='$search_propsubid' AND `BEDROOMS`='$bedrooms' AND `TRANS_TYPE_ID`='1' HAVING `PRICE` BETWEEN '$pricefrom' AND '$priceto' ORDER BY `UPDATE_DATE` DESC");

感谢您的时间,我希望有人能指出我正确的方向.. 问候 史蒂夫

2 个答案:

答案 0 :(得分:1)

您可以尝试implode数组:

$search_propsubid = array('1,2,3,4,5,6,21,22,23,24,26,27,30');
$comma_separated = implode(",", $search_propsubid);
$sql = mysql_query("SELECT * FROM `properties` WHERE `PROP_SUB_ID` in ($comma_separated) ...

如果这不适合你,请回复新闻。

答案 1 :(得分:1)

您可以使用MySql IN()比较运算符选择与值列表匹配的所有内容:

$sql = mysql_query("
  SELECT * 
  FROM `properties` 
  WHERE `PROP_SUB_ID` IN (" .implode(",", $search_propsubid). ")
  AND `BEDROOMS`='$bedrooms' 
  AND `TRANS_TYPE_ID`='1' 
  HAVING `PRICE` BETWEEN '$pricefrom' AND '$priceto' 
  ORDER BY `UPDATE_DATE` DESC
");

假设$proptype == 'flat',输出将是:

SELECT *
FROM `properties` 
WHERE `PROP_SUB_ID` IN (7,8,9,10,11,28,29,44) 
...