SQL查询空字段,选择所有选项如*

时间:2013-10-14 21:24:12

标签: php mysql sql

php搜索页面使用SQL查询根据设置的条件搜索属性,在下面的例子中选择了街道“Harrison Road”:

search.php?city=1&rooms=3&rooms_upper=7&price_lower=55&price_upper=100&streetname=Harrison%20Road

我正在寻找一种方法将街道名称留空并仍然按以下方式选择所有记录:

 search.php?city=1&rooms=3&rooms_upper=7&price_lower=55&price_upper=100&streetname=

我尝试使用'*'符号和其他SQL ocmmands一样指定SELECT ALL但它在这个实例中不起作用。

问题是当街道选项留空时,搜索不会显示任何结果。

我希望使用空街道条件运行搜索的原因是因为在用户使用页面上的下拉选项选择特定街道之前加载search.php。

我希望使用指定的第一个条件搜索所有记录: rooms,rooms_upper,price_lower,price_upper

为了在用户缩小搜索范围之前显示所有记录,标准搜索页面加载保留了最宽的搜索条件(3< rooms< 7)和(£55< rent<£75)如果需要,指定特定“街道名称”的标准。

非常感谢! Jeanclaude

完整的SQL在这里:

    $sql=mysql_query("SELECT main_table.housenumber, main_table.housenumber, main_table.streetname,
                      max(main_table.rent) AS reviews_rent, main_table.rooms AS reviews_rooms,main_table.average, 
                      houses.houseID, houses.path, houses.rooms AS houses_rooms,houses.rent AS houses_rent
                    FROM main_table 
                      LEFT JOIN houses ON main_table.housenumber = houses.housenumber 
                           AND main_table.streetname = houses.streetname
                    WHERE main_table.streetname='$page_streetname' 
                        AND main_table.city=$city 
                        AND main_table.verify='1' 
                        AND main_table.rooms>='$rooms' 
                        AND main_table.rooms<='$rooms_upper' 
                        AND main_table.rent>=$price_lower 
                        AND main_table.rent<=$price_upper
                    GROUP BY main_table.housenumber, main_table.streetname 
                    ORDER BY average DESC, houseID DESC, reviewID DESC;");

我想在WHERE子句中保留streetname,但如果将其留空,我不想限制搜索。

2 个答案:

答案 0 :(得分:1)

如何动态创建查询。

在WHERE子句中设置不带streetname的基本查询,但包含sprintf字符串标记。 然后,如果$ page_streetname有一个值,则动态地将街道名称选择添加到查询中,或者不添加任何内容。

$q = "
SELECT main_table.housenumber, 
       main_table.streetname, 
       Max(main_table.rent) AS reviews_rent, 
       main_table.rooms     AS reviews_rooms, 
       main_table.average, 
       houses.houseid, 
       houses.path, 
       houses.rooms         AS houses_rooms, 
       houses.rent          AS houses_rent 
FROM   main_table 
       LEFT JOIN houses 
              ON main_table.housenumber = houses.housenumber 
                 AND main_table.streetname = houses.streetname 
WHERE  %s
       main_table.city = $city 
       AND main_table.verify = '1' 
       AND main_table.rooms >= '$rooms' 
       AND main_table.rooms <= '$rooms_upper' 
       AND main_table.rent >=$ price_lower 
       AND main_table.rent <=$ price_upper 
GROUP  BY main_table.housenumber, 
          main_table.streetname 
ORDER  BY average DESC, 
          houseid DESC, 
          reviewid DESC";

$q = isset($page_streetname) 
          ? sprintf( $q, "main_table.streetname = '$page_streetname' AND " )
          : sprintf( $q, '');

答案 1 :(得分:0)

使用PHP很容易解决(不需要sprintf()):

<?php

$page_streetname = empty($page_streetname)
  ? null
  : "AND main_table.streetname = '{$page_streetname}'"
;

$result = mysql_query(
  "SELECT
    main_table.housenumber,
    main_table.housenumber,
    main_table.streetname,
    max(main_table.rent) AS reviews_rent,
    main_table.rooms AS reviews_rooms,
    main_table.average,
    houses.houseID,
    houses.path,
    houses.rooms AS houses_rooms,
    houses.rent AS houses_rent
  FROM main_table
    LEFT JOIN houses
      ON main_table.housenumber = houses.housenumber
        AND main_table.streetname = houses.streetname
  WHERE
    main_table.city = '{$city}'
    AND main_table.verify = 1 
    AND main_table.rooms >= '{$rooms}'
    AND main_table.rooms <= '{$rooms_upper}'
    AND main_table.rent >= {$price_lower}
    AND main_table.rent <= {$price_upper}
    {$page_streetname}
  GROUP BY
    main_table.housenumber,
    main_table.streetname 
  ORDER BY
    average DESC,
    houseID DESC,
    reviewID DESC"
);

?>