我的项目中有旧版本(1.2)Propel。我想查询:
SELECT city_id, COUNT(*) FROM House GROUP BY city_id
我试过:
$c = new Criteria;
$c->addAsColumn('count', 'Count(' . HousePeer::CITY_ID . ')');
$this->results = HousePeer::doSelect($c);
但这不起作用 - 只返回数据库中的第一条记录,没有计数等。
我也试过:
$con = Propel::getConnection();
$sql = "SELECT city_id, COUNT(*) FROM House GROUP BY city_id";
$stmt = $con->createStatement();
$rs = $stmt->executeQuery($sql, ResultSet::FETCHMODE_NUM);
$this->results = HousePeer::populateObjects($rs);
但这让我
Error populating House object [wrapped: Invalid resultset column: 3]
我只想收到与 SQL 表中相同的内容:
city_id | count
1 | 2
2 | 4
3 | 3
ETC
或从 CityPeer 获取名称 City (我在 City 和 House 之间有正确的关系),例如:
city | count
New York | 2
Paris | 4
Washington | 3
但我不能在 Propel 中使用这个查询。