我有一张像
这样的表格+----+----------+------------+
| id | order_id | service_id |
+----+----------+------------+
| 1 | 3423343 | 123 |
+----+----------+------------+
| 2 | 3432356 | 124 |
+----+----------+------------+
| 3 | 2345643 | 123 |
+----+----------+------------+
| 4 | 2313343 | 125 |
+----+----------+------------+
我想得到的是每个service_id
的计数,它将提供这样的输出
+------------+------+
| service_id | cout |
+------------+------+
| 123 | 2 |
+------------+------+
| 124 | 1 |
+------------+------+
| 125 | 1 |
+------------+------+
并将其安排在Highest -> Lowest
订单
我在CakePHP 3.2工作
答案 0 :(得分:1)
在列service_id
上使用group by并计算每个组的行数。
SELECT
service_id,
COUNT(service_id) AS `Count`
FROM
tablename -- your table name
GROUP BY
service_id
ORDER BY
`Count` DESC;