简体   繁体   English

使用现有字段值的MySQL Alter Table

[英]MySQL Alter Table using existing field values

I am working with a database that has a table called date, which contains a separate field for day, month, year. 我正在使用一个具有称为日期的表的数据库,该表包含一个单独的字段,分别用于日,月,年。 Clearly this is not ideal when I am trying to run comparisons, etc. I am wondering is it possible for me to add a DateTime field to each row of this table and insert a concatenated string into the new field from the existing day, month, year fields. 显然,当我尝试进行比较等操作时,这并不理想。我想知道是否可以将DateTime字段添加到该表的每一行,并从现有的日期,月份,年字段。

I am quite sure its possible, I'm just wondering if anyone might be able to point me in the right direction on how to achieve this? 我很确定这是有可能的,我只是想知道是否有人可以向我指出如何实现这一目标的正确方向?

Below is the current date table: (i know) 以下是当前日期表:(我知道)

CREATE TABLE IF NOT EXISTS `date` (
  `deposition_id` varchar(11) NOT NULL default '',
  `day` int(2) default NULL,
  `month` int(2) default NULL,
  `year` int(4) default NULL,
  PRIMARY KEY  (`deposition_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

first use alter table query: 首先使用alter table查询:

alter table date add column datetimefield datetime NOT Null 更改表日期添加列datetimefield datetime NOT Null

then 然后

use update query with self join on date and update datetimefield with concat on date,month, year column values. 使用带有日期自动联接的更新查询,以及带有日期,月份,年份列值的concat的datetimefield更新。

试试这个(未试用)-

UPDATE date d SET d.datetime = (SELECT CONCAT('-','year','month','day') from date d1 where d1.id = d.id);

What is the problem, I don't understand? 有什么问题,我不明白? Alter the table, add new DATE column and then populate it with a string "yyyy-mm-dd" using CONCAT mysql function or whatever. 更改表,添加新的DATE列,然后使用CONCAT mysql函数或其他任何字符串填充“ yyyy-mm-dd”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM