我使用的是mysql,我有一个表,其中包含以下mysql>descripe activities;
+------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| year | int(11) | NO | MUL | NULL | |
| month | int(11) | NO | | NULL | |
| project_id | int(10) unsigned | NO | MUL | NULL | |
| user_id | int(10) unsigned | NO | MUL | NULL | |
| task_hour | double(8,2) | NO | | NULL | |
| from_otl | tinyint(1) | NO | | 0 | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
+------------+------------------+------+-----+---------+----------------+
9 rows in set (0.01 sec)
这给了我一个特定的年,一个特定的月,一个特定的项目,一个特定的用户,工作小时数。
我需要用上一个表中的数据创建一个具有不同结构的表。这将有相同的列,但不是月和任务时数,我将每个月作为一个列和工作时数在此列。
以下是我尝试的:
CREATE TEMPORARY TABLE temp_a
(
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
year INT(10),
project_id INT(10),
user_id INT(10),
jan_com double(8,2),
jan_otl tinyint(1),
feb_com double(8,2),
feb_otl tinyint(1),
mar_com double(8,2),
mar_otl tinyint(1),
apr_com double(8,2),
apr_otl tinyint(1),
may_com double(8,2),
may_otl tinyint(1),
jun_com double(8,2),
jun_otl tinyint(1),
jul_com double(8,2),
jul_otl tinyint(1),
aug_com double(8,2),
aug_otl tinyint(1),
sep_com double(8,2),
sep_otl tinyint(1),
oct_com double(8,2),
oct_otl tinyint(1),
nov_com double(8,2),
nov_otl tinyint(1),
dec_com double(8,2),
dec_otl tinyint(1)
);
ALTER TABLE `temp_a` ADD UNIQUE( `year`,`project_id`, `user_id`);
INSERT INTO temp_a (`year`,`project_id`,`user_id`) VALUES (SELECT `year`,`project_id`,`user_id` FROM `activities` group by `year`,`project_id`,`user_id`);
select * from temp_a;
但它不接受插入。。。
我想做的是首先在这个临时表中填写所有唯一的年份、项目id、用户id,然后通过遍历activities表更新每个记录,并用正确月份的值更新正确的行。
1条答案
按热度按时间7vux5j2d1#
你需要移除
VALUES
使用时SELECT
在INSERT INTO
```INSERT INTO temp_a (
year
,project_id
,user_id
)(SELECT
year
,project_id
,user_id
FROMactivities
group byyear
,project_id
,user_id
);