使用其他数据将行从表移到另一个表

时间:2013-07-04 16:34:37

标签: mysql

我有两张表看起来类似于这些表

成员

CREATE TABLE IF NOT EXISTS `members` (
    `ID` int(10) unsigned NOT NULL auto_increment,
    `email` varchar(255) NOT NULL,
    `password` varchar(100) NOT NULL,
    PRIMARY KEY  (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

已归档的会员

CREATE TABLE IF NOT EXISTS `archived_members` (
    `ID` int(10) unsigned NOT NULL,
    `email` varchar(255) NOT NULL,
    `password` varchar(100) NOT NULL,
    `archived_date` timestamp NOT NULL,
    `archived_type` varchar(20) NOT NULL
) ENGINE=ARCHIVE DEFAULT CHARSET=utf8 ;

我通常使用此查询语句在表之间移动行 这只是一个例子

INSERT INTO `archived_members` (`ID`, `email`, `password`) SELECT * FROM `members` WHERE `ID`=1;
DELETE FROM `members` WHERE `ID`=1;

但在我的情况下(本案例),我必须在archived_members表中为archived_date&表中的其他字段指定值。 archived_type。因此,我试过

INSERT INTO `archived_members` (`ID`, `email`, `password`) SELECT * FROM `members` WHERE `ID`=1;
UPDATE `archived_members` SET `archived_date`=CURRENT_DATE(), `archived_type`='reason of archving' WHERE `ID`=1;
DELETE FROM `members` WHERE `ID`=1;

不幸的是,archived_members表的引擎是 ARCHIVE ,因为我认为它应该是,因此在将行插入表后我无法更新

请帮助!

2 个答案:

答案 0 :(得分:0)

试试这个:

INSERT INTO `archived_members` (`ID`, `email`, `password`,`archived_date`,`archived_type`) SELECT m.*, CURRENT_DATE(), 'reason of archving' FROM `members` m WHERE `ID`=1;
DELETE FROM `members` WHERE `ID`=1;

我没有测试,但我很确定这可行。

答案 1 :(得分:0)

您知道SELECT * FROM...不是撰写SELECT声明的唯一方法,对吗?

INSERT INTO `archived_members` 
 (`ID`, `email`, `password`, `archived_date`, `archived_type` ) 
SELECT 
 `ID`, `email`, `password`, CURRENT_DATE(), 'reason of archving' 
FROM `members` WHERE `ID`=1;