mysql查询将唯一行移动到临时表

时间:2013-08-07 15:03:06

标签: mysql

我想创建一个查询,将每个唯一条目从一个表移动到另一个临时表。 我创建了以下查询来获取唯一的信息:

select date, messageType, zCampaignId, isSenderPolicy, sender, recipient, policy, operator, country, zNumber 
from topcampaigns_HOUR 
group by date, messageType, zCampaignId, isSenderPolicy, sender, recipient, policy, operator, country, zNumber 
having count(*) = 1;

并返回大量结果:

/* Affected rows: 0  Found rows: 473  Warnings: 0  Duration for 1 query: 0.000 sec. (+ 0.016 sec. network) */

但是当我把它放到更新中时,我似乎无法更新tmp表:

UPDATE topcampaigns_HOUR_tmp as b 
INNER JOIN (
   select date, messageType, zCampaignId, isSenderPolicy, sender, recipient, policy, operator, country, zNumber 
   from topcampaigns_HOUR 
   group by date, messageType, zCampaignId, isSenderPolicy, sender, recipient, policy, operator, country, zNumber 
   having count(*) = 1) as a 
SET 
b.date=a.date, 
b.messageType=a.messageType, 
b.zCampaignId=a.zCampaignId, 
b.isSenderPolicy=a.isSenderPolicy, 
b.sender=a.sender, 
b.recipient=a.recipient, 
b.policy=a.policy,
b.operator=a.operator,
b.country=a.country, 
b.zNumber=a.zNumber;

导致:

/* Affected rows: 0  Found rows: 0  Warnings: 0  Duration for 1 query: 00:04:25 */

我尝试过很多东西,比如使用:

UPDATE topcampaigns_HOUR_tmp JOIN (select date, messageType, zCampaignId, isSenderPolicy, sender, recipient, policy, operator, country, zNumber from topcampaigns_HOUR group by date, messageType, zCampaignId, isSenderPolicy, sender, recipient, policy, operator, country, zNumber having count(*) = 1) a SET 
topcampaigns_HOUR_tmp.date=a.date, 
topcampaigns_HOUR_tmp.messageType=a.messageType, 
topcampaigns_HOUR_tmp.zCampaignId=a.zCampaignId, 
topcampaigns_HOUR_tmp.isSenderPolicy=a.isSenderPolicy, 
topcampaigns_HOUR_tmp.sender=a.sender, 
topcampaigns_HOUR_tmp.recipient=a.recipient, 
topcampaigns_HOUR_tmp.policy=a.policy, 
topcampaigns_HOUR_tmp.operator=a.operator, 
topcampaigns_HOUR_tmp.country=a.country, 
topcampaigns_HOUR_tmp.zNumber=a.zNumber;

我又得到了:

/* Affected rows: 0  Found rows: 0  Warnings: 0  Duration for 1 query: 00:03:42 */

topcampaigns_HOUR_tmp表似乎永远不会更新。

我做错了什么?

A

1 个答案:

答案 0 :(得分:2)

如果您要添加行,则需要INSERT,如果要从查询结果中添加行,则需要INSERT ... SELECT。尝试这样的事情:

INSERT INTO topcampaigns_HOUR_tmp (
   date, messageType, zCampaignId, isSenderPolicy, sender,
   recipient, policy, operator, country, zNumber)
select
   date, messageType, zCampaignId, isSenderPolicy, sender,
   recipient, policy, operator, country, zNumber 
from topcampaigns_HOUR 
group by
   date, messageType, zCampaignId, isSenderPolicy, sender,
   recipient, policy, operator, country, zNumber 
having count(*) = 1