我正在尝试运行以下查询,并将结果放入名为weekly_summary_activities的表中。
Select
email, campaign, type,
year, week, business_comp,
activity, SUM(hours)
FROM activity_log
Group By email, campaign, project,
business_comp, activity
weekly_summary_activities的结构为
+--------+---------------+-------------+-------------------+------------+------+---------+----------+----------------+
| # | Name | Type | Collation | Attributes | Null | Default | Comments | Extra |
+--------+---------------+-------------+-------------------+------------+------+---------+----------+----------------+
| 1 | idPrimary | tinyint(4) | | | No | None | | AUTO_INCREMENT |
| 2 | email | varchar(30) | latin1_swedish_ci | | No | None | | |
| 3 | campaign | varchar(30) | latin1_swedish_ci | | No | None | | |
| 4 | type | varchar(3) | latin1_swedish_ci | | No | None | | |
| 5 | year | varchar(4) | latin1_swedish_ci | | No | None | | |
| 6 | week | int(3) | | | No | None | | |
| 7 | business_comp | varchar(30) | latin1_swedish_ci | | No | None | | |
| 8 | project | varchar(30) | latin1_swedish_ci | | No | None | | |
| 9 | activity | varchar(30) | latin1_swedish_ci | | No | None | | |
| 10 | hours | int(8) | | | No | None | | |
+--------+---------------+-------------+-------------------+------------+------+---------+----------+----------------+
我尝试过:
INSERT INTO weekly_summary_activities
SELECT email,
campaign,
type,
year,
week,
business_comp,
activity,
Sum(hours)
FROM activity_log
GROUP BY email,
campaign,
project,
business_comp,
activity
但出现错误“#1136-列数与第1行的值数不匹配”。我究竟做错了什么?似乎只是插入不起作用。我可以在phpMyAdminSQL的SQL选项卡中运行查询(执行插入操作),这似乎可行。
答案 0 :(得分:1)
您应在insert into子句中指定列,例如:
INSERT INTO weekly_summary_activities (email, campaign, type, year, week, business_comp, activity, hours)
SELECT email, campaign, type, year, week, business_comp, activity, SUM(hours)
FROM activity_log
GROUP BY email, campaign, project, business_comp, activity
您遇到了问题,因为该表包含一个Id列。表具有比插入子句更多的列。