1

我有两个表看起来与这些表相似

成员

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;

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

请帮忙!

4

2 回答 2

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;

我没有测试,但我很确定这可以工作。

于 2013-07-04T16:39:02.183 回答
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;
于 2013-07-04T16:40:19.417 回答