我試圖從我的user
表中的兩列(user_id
,team_id
)中插入值到company_team
的結表。但是,我通過兩次不同的嘗試將created_at
和updated_at
列的值插入到company_team
中,並且不知道如何處理它們。SQL插入時間戳問題
在第一次嘗試,我只是想與從user
表中的等價字段中的值更新兩個領域:(注列在兩個表是DATETIME)
INSERT INTO `company_team` (`created_at`,`updated_at`,`user_id`, `company_id`)
SELECT `user_id`, `company_id`, `created_at`, `updated_at`
FROM `user` AS `u`
WHERE `u`.`company_id` IS NOT NULL;
錯誤:
Incorrect datetime value: '1' for column 'created_at' at row 1
然後我試圖只用在創建記錄當前的日期/時間:
INSERT INTO `company_team` (NOW(),NOW(),`user_id`, `company_id`)
SELECT `user_id`, `company_id`, `created_at`, `updated_at`
FROM `user` AS `u`
WHERE `u`.`company_id` IS NOT NULL;
錯誤:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NOW(),NOW(),`user_id`, `organization_id`)
SELECT `user_id`, `company_id`, `' at line 1
謝謝!記住列的排序是嚴格的。 – cphill