mysql存储过程,查询多个重复的数据数据出现次数

比如我有表A和B,其中表A有字段a,表B有字段b,c
现在我需要查找数据库中表A中
a出现次数大于1的数据的数量赋值给表B的b,
a出现次数等于1的数据的数量赋值给表B的c

请问如何实现啊?
问题是,没有什么当前时间,同一个电话号码,做多次呼叫,我需要用存储过程,判断表可能出现的同一个号码N次呼叫中任意2次呼叫是否在2小时内,如果在两小时内的话算1次。也就是我要用存储过程批量的按条件处理数据,数据是按2小时作为条件,根据同一个电话号码的不同呼叫时间来处理的

第1个回答  2015-01-16
update B
set b = (select count(*)
from A
group by a
having count(*) > 1),
c = (select count(*)
from A
group by a
having count(*) = 1)追问

插入B表中,不是更新

追答

insert into B(b, c)
select count(*), null
from A
group by a
having count(*) > 1
union all
select null, count(*)
from A
group by a
having count(*) = 1

追问

比如客服接电话
如果表A有:客服id,呼叫时间,电话号码
B表3列分别放:客服id;2小时内重复呼叫量;2小时内未重复呼入的量

该如何写存储过程,将A中符合B中条件的数据,放到B中。

这是另一个问题,追加20分

表A:员工id,呼叫时间,呼叫号码
表B:员工id,2小时重复呼叫数量,2小时不重复呼叫数量

现在要将A表中的数据按照“同一个号码2小时是否重复呼叫”放到B表中。咋做?

追答

现在要将A表中的数据按照“同一个号码2小时是否重复呼叫”放到B表中。咋做?
update B
set 2小时内重复呼叫量 = (select count(*)
from A
where B.客服id = A.客服id
and 呼叫时间 >= sysdate + 1/24 * 2
group by 电话号码
having count(*) > 1),
2小时内未重复呼入的量 = (select count(*)
from A
where B.客服id = A.客服id
and 呼叫时间 >= sysdate + 1/24 * 2
group by 电话号码
having count(*) = 1);
否重复呼叫 = (select case when count(*) > 1 then '有重复' else '没有重复' end
from A
where B.客服id = A.客服id
and 呼叫时间 >= sysdate + 1/24 * 2
group by 电话号码
having count(*) > 1);

追问

and 呼叫时间 >= sysdate + 1/24 * 2 是什么意思??

追答

and 呼叫时间 >= sysdate + 1/24 * 2

不好意思写错了,
应该是
and 呼叫时间 >= sysdate - 1/24 * 2

例如
呼叫时间0点 >= 当前是3点 - 2小时
0> 1 不符合的,也就是0点的不要

本回答被网友采纳