我有这个问题:
$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)
有谁知道我如何跟踪/返回城市相同的行数?
答案 0 :(得分:1)
答案 1 :(得分:0)
GROUP BY
和COUNT()
应该可以满足您的需求:
$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