Py学习  »  DATABASE

mysql sum查询

Debugger • 5 年前 • 1569 次点击  

我在写一个PHP工具, 从我的查询中,我得到低于输出的结果

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

我只需要10位数的输出。

例如: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;
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/39020
 
1569 次点击  
文章 [ 2 ]  |  最新文章 5 年前
Mureinik
Reply   •   1 楼
Mureinik    6 年前

你可以治疗 dst 作为一个字符串,取其最右边的四个字符:

SEECT    RIGHT(dst, 4), 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 RIGHT(dst,4)
ORDER BY 1;
fa06
Reply   •   2 楼
fa06    6 年前

您可以尝试使用 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