mysql:截断字段并聚合另一个表中的数据

时间:2013-08-26 09:15:47

标签: mysql sql

我有一张表格,其日期时间为日期。

mysql> select * from topsmsspammers_hour limit 1;
+---------------------+------+---------+-------+--------------+---------------------------------------------+-------------+--------+----------------+
| date                | spam | suspect | clean | spammer      | spam_campaign_id                            | messageType | policy | isSenderPolicy |
+---------------------+------+---------+-------+--------------+---------------------------------------------+-------------+--------+----------------+
| 2013-06-11 23:00:00 |    1 |       0 |     0 | +12073708065 | NotSet|Message35392HerbisReallyWorkingRealy |           4 |      1 |              1 |
+---------------------+------+---------+-------+--------------+---------------------------------------------+-------------+--------+----------------+
1 row in set (0.00 sec)

mysql>

我想使用此表中的数据来填充具有完全相同列名但具有截断日期的聚合日期日期表(并更改计数以反映更改) 我首先确定了将截断我需要的数据的select查询:

mysql> select SUBSTRING_INDEX(date, ' ', 1) as date, sum(spam) as spam, sum(suspect) as suspect, sum(clean) as clean, spammer, spam_campaign_id, messageType, policy, isSenderPolicy from topsmsspammers_hour group by date, spammer, spam_campaign_id, messageType, policy, isSenderPolicy limit 1;
+------------+------+---------+-------+--------------+---------------------------------------------+-------------+--------+----------------+
| date       | spam | suspect | clean | spammer      | spam_campaign_id                            | messageType | policy | isSenderPolicy |
+------------+------+---------+-------+--------------+---------------------------------------------+-------------+--------+----------------+
| 2013-06-11 |    1 |       0 |     0 | +12073708065 | NotSet|Message35392HerbisReallyWorkingRealy |           4 |      1 |              1 |
+------------+------+---------+-------+--------------+---------------------------------------------+-------------+--------+----------------+
1 row in set, 1 warning (1.66 sec)

mysql>

然后,我尝试使用该选择查询来填充新表,但未插入截断日期。而是插入了原始日期:

mysql> insert into topsmsspammers_day (date, spam, suspect, clean, spammer, spam_campaign_id, messageType, policy, isSenderPolicy) select SUBSTRING_INDEX(date, ' ', 1) as date, sum(spam) as spam, sum(suspect) as suspect, sum(clean) as clean, spammer, spam_campaign_id, messageType, policy, isSenderPolicy from topsmsspammers_hour group by date, spammer, spam_campaign_id, messageType, policy, isSenderPolicy limit 1;
Query OK, 1 row affected, 1 warning (1.68 sec)
Records: 1  Duplicates: 0  Warnings: 1

mysql> select * from topsmsspammers_day limit 1;                                                                +---------------------+------+---------+-------+--------------+---------------------------------------------+-------------+--------+----------------+
| date                | spam | suspect | clean | spammer      | spam_campaign_id                            | messageType | policy | isSenderPolicy |
+---------------------+------+---------+-------+--------------+---------------------------------------------+-------------+--------+----------------+
| 2013-06-11 00:00:00 |    1 |       0 |     0 | +12073708065 | NotSet|Message35392HerbisReallyWorkingRealy |           4 |      1 |              1 |
+---------------------+------+---------+-------+--------------+---------------------------------------------+-------------+--------+----------------+
1 row in set (0.00 sec)

mysql>

我尝试将数据更改为具有不同的名称以填充新表格,但这仍然导致将完整日期插入到表格中:

mysql> insert into topsmsspammers_day (date, spam, suspect, clean, spammer, spam_campaign_id, messageType, policy, isSenderPolicy) select SUBSTRING_INDEX(date, ' ', 1) as dayDate, sum(spam) as spam, sum(suspect) as suspect, sum(clean) as clean, spammer, spam_campaign_id, messageType, policy, isSenderPolicy from topsmsspammers_hour group by dayDate, spammer, spam_campaign_id, messageType, policy, isSenderPolicy limit 1;
Query OK, 1 row affected (1.75 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> select * from topsmsspammers_day limit 1;
+---------------------+------+---------+-------+--------------+---------------------------------------------+-------------+--------+----------------+
| date                | spam | suspect | clean | spammer      | spam_campaign_id                            | messageType | policy | isSenderPolicy |
+---------------------+------+---------+-------+--------------+---------------------------------------------+-------------+--------+----------------+
| 2013-06-11 00:00:00 |    1 |       0 |     0 | +12073708065 | NotSet|Message35392HerbisReallyWorkingRealy |           4 |      1 |              1 |
+---------------------+------+---------+-------+--------------+---------------------------------------------+-------------+--------+----------------+
1 row in set (0.00 sec)

mysql>

我甚至尝试更改插入中的名称,但是这个名称不是新表中的字段:

mysql> insert into topsmsspammers_day (dayDate, spam, suspect, clean, spammer, spam_campaign_id, messageType, policy, isSenderPolicy) select SUBSTRING_INDEX(date, ' ', 1) as dayDate, sum(spam) as spam, sum(suspect) as suspect, sum(clean) as clean, spammer, spam_campaign_id, messageType, policy, isSenderPolicy from topsmsspammers_hour group by dayDate, spammer, spam_campaign_id, messageType, policy, isSenderPolicy limit 1;
ERROR 1054 (42S22): Unknown column 'dayDate' in 'field list'
mysql>

我错过了什么?如何将截断的聚合数据插入新表?

A

它看起来像2013-06-11 23:00:00被改为2013-06-11 00:00:00所以也许它的dataType有问题 甲

1 个答案:

答案 0 :(得分:1)

检查列的数据类型,我猜您正在使用日期时间。将其更改为Date数据类型