我正在使用此查询从已连接的表中进行选择,如果是新的则插入,如果是重复则更新:
Insert into test.ambition_test(ExtID, Extension,
Total_Talk_Time_seconds,Total_Talk_Time_minutes,Total_Outbound,
Total_Inbound, Missed_Calls, Total_Calls, Time_of_report )
SELECT
RESPONSIBLEUSEREXTENSIONID as ExtID
, c.extension as Extension
, sum(Duration) as Total_Talk_Time_seconds
, round(sum(Duration) / 60,2) as Total_Talk_Time_minutes
, sum(if(LEGTYPE1 = 1,1,0)) as Total_Outbound
, sum(if(LEGTYPE1 = 2,1,0)) as Total_Inbound
, sum(if(Answered = 1,0,1)) as Missed_Calls
, count(DISTINCT b.NOTABLECALLID) as Total_Calls
, NOW()
FROM cdrdb.session a
JOIN cdrdb.callsummary b
ON a.NOTABLECALLID = b.NOTABLECALLID
join cdrdb.mxuser c
ON a.RESPONSIBLEUSEREXTENSIONID = c.EXTENSIONID
-- WHERE b.ts > (now() - interval 15 minute)
WHERE b.ts >= curdate()
GROUP BY a.RESPONSIBLEUSEREXTENSIONID
ORDER BY Total_Talk_Time_minutes DESC
ON duplicate key update Total_Talk_Time_seconds = Total_Talk_Time_seconds,
Total_Talk_Time_minutes = Total_Talk_Time_minutes, Total_Outbound =
Total_Outbound, Total_Inbound = Total_Inbound
, Missed_calls = Missed_Calls, Total_Calls = Total_Calls, Time_of_report =
Time_of_report;
我今天已经运行了几次,每次它似乎都会添加一些新的记录,但它似乎没有更新任何早期的记录,即使它应该。
这里的一种可能性是,我希望这可以将“扩展”字段,或者甚至是与“ExtID”相结合的字段视为副本。 ID是我在test.ambition_test表中的主键字段,它会自动递增,但如果它们已经在表中,我希望根据扩展名进行更新,而不仅仅是插入新的。
如果我截断表并再次运行初始查询,它将执行初始插入,并且值远远高于先前的查询,这是正确的。但是如果我在接下来的几个小时内运行查询,它只会添加新的扩展记录但根本没有更新。
我在MySQL工作台中修改了我的表并使'ExtID'和'Extension'不是NUll和Unique Index但是我不知道我是否应该做任何其他事情。是否有理由不更新现有记录?