我有一个MySQL
表格,其中我在其他列旁边有一个year
和一个month
列。我必须用1个DATE类型列替换这两个INT类型列,但我必须保留并转换旧记录值。旧纪录上的那一天总是01.
例如: 这就是我现在所拥有的
[project_planned_cost]
ppc_id | year | month |
-----------------------
0 | 2012 | 5
1 | 2013 | 11
2 | 2008 | 2
这就是我想要实现的目标:
[project_planned_cost]
ppc_id | date
--------------
0 | 2012-05-01
1 | 2013-11-01
2 | 2008-02-01
我已经想出如何生成日期,但我不知道如何将它应用于每一行:
ALTER TABLE project_planned_cost ADD COLUMN `date` DATE;
SELECT
CONCAT(`year`, '-', LPAD(`month`, 2, '0'), '-', '01') AS 'date'
FROM project_planned_costs
如果你能给我一些建议,我会很感激
答案 0 :(得分:2)
ALTER TABLE project_planned_cost ADD COLUMN `date` DATE;
update project_planned_cost set date =
CONCAT(`year`, '-', LPAD(`month`, 2, '0'), '-', '01') ;
答案 1 :(得分:0)
probably your looking for like this
//Create the column:
alter project_planned_cost add column date varchar(50);
// Update the current values
update project_planned_cost set date = concat(year, ' - ', month, '- ', '01');
Hope this link help you more
http://stackoverflow.com/questions/5774532/mysql-combine-two-columns-and-add-into-a-
new-column