使用参数计算行数

时间:2011-12-27 21:40:59

标签: php mysql count rows

我有这个问题:

$queryf = "SELECT * FROM classifieds 
WHERE city IN ('$cities') AND state IN ('$states') 
ORDER BY FIELD (city, '$cities'), datetime DESC";

$ cities是5英里范围内的一系列附近城市。与州相同。

基本上,我想搜索具有用户输入信息的用户信息表(如craigslist / classifieds类型表),并计算PER CITY的行数。因此,有可能回应如下:

10 results found within 5 miles:
Dublin, CA (8 records)
Hayward, CA (2 records)

有谁知道我如何跟踪/返回城市相同的行数?

3 个答案:

答案 0 :(得分:1)

MySQL为这种聚合提供了大量功能和关键字。

阅读the documentation for your SELECT statement,特别是在谈到GROUP BY条款时。

答案 1 :(得分:0)

GROUP BYCOUNT()应该可以满足您的需求:

$queryf = "SELECT *, count(classifieds.id) AS num_cities FROM classifieds 
WHERE city IN ('$cities') AND state IN ('$states') 
GROUP BY city
ORDER BY FIELD (city, '$cities'), datetime DESC";

答案 2 :(得分:0)

尝试使用GROUP BY

SELECT city, COUNT(1) AS count FROM classifieds 
WHERE city IN ('$cities') AND state IN ('$states') 
GROUP BY city