2

我有一个表格,其中包含日期时间形式的日期。

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>

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

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>

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

一种

看起来 2013-06-11 23:00:00 已更改为 2013-06-11 00:00:00 所以可能是数据类型 A 的问题

4

1 回答 1

1

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

于 2013-08-26T09:29:49.153 回答