MYSQL总和查询

时间:2019-03-21 08:39:06

标签: php mysql

我正在写一个php工具, 从查询中我得到以下输出

| dst        | count(*) |
| 0280302002 |       31 |
| 0280304115 |       83 |
| 0280304121 |       80 |
| 0280304124 |       10 |
| 0280304136 |        1 |
| 2002       |        3 |
| 4115       |       12 |
| 4121       |       10 |
| 4124       |        2 |

我只需要输出10位数字即可。

Ex:-31(0280302002的值+ 3(2002的值)=输出应为  0280302002 = 34

有什么办法可以得到这个值?

我的查询

select dst,count(*) from cdr where calldate between '2019-01-01 00:00:00' and '2019-01-31 23:59:59' and dst in ('4121', '2006','2011''4124','0280304121', '0280302006','0280302011','0280304124') and length(src)>4 group by dst order by dst;

2 个答案:

答案 0 :(得分:1)

您可以尝试使用case when表达式和concat()函数

select case when length(dst)=4 then concat('028030',dst) else dst end as dst,count(*) 
from cdr 
where calldate between '2019-01-01 00:00:00' and '2019-01-31 23:59:59' and 
dst in ('4121', '2006','2011''4124','0280304121', '0280302006','0280302011','0280304124') and length(src)>4 
group by case when length(dst)=4 then concat('028030',dst) else dst end
order by dst

答案 1 :(得分:0)

您可以将result = pdist(a(:), @times); 视为字符串,并使用其最右边的四个字符:

dst