如何显示选择计数的最后一行而不是第一行?

时间:2018-07-30 15:59:47

标签: php mysql count

我有这张桌子line 6, in <module> TypeError: 'int' object is not callable

calls

我正在执行此查询,以计算每个销售人员每个客户的通话次数

id | clientid | salespersonid | duration | statusid | last_update
1  | c11      | sp99          | 05:00    | 1        | yyyy
2  | c11      | sp99          | 06:00    | 1        | yyyy
3  | c11      | sp99          | 07:00    | 3        | yyyy
4  | c12      | sp99          | 08:00    | 3        | yyyy

这将返回结果

select *,count(wpc.id) as num_of_calls 
                                    from calls wpc
                                    where wpc.salespersonid=?
                                    group by wpc.clientid
                                    order by wpc.last_update desc

查询很好,但是我对程序计数和显示行的顺序不满意。它显示计数的第一行。我希望它显示每个计数的最后一行。

所以结果应该是

id | clientid | salespersonid | duration | statusid | last_update|num_of_calls
1  | c11      | sp99          | 05:00    | 1        | yyyy       |3
4  | c12      | sp99          | 08:00    | 3        | yyyy       |1

如何显示每个计数的最后一行而不是第一行?

1 个答案:

答案 0 :(得分:0)

Using ORDER BY and GROUP BY together重复

使用解决方案:https://stackoverflow.com/a/35053900/2397717

 SELECT wpc.*, COUNT(wpc.id) AS num_of_calls FROM (SELECT * FROM calls ORDER BY last_update DESC) as wpc GROUP BY wpc.clientid