-1

嗨,我正在尝试在使用以下代码截断表格之前保留表格的备份。

IF EXISTS (`emails3`) THEN
    SELECT * INTO `emails3` 
    FROM `emails1`;

    SELECT * INTO `emails3` 
    FROM `emails2` 
    WHERE NOT `word` IN (SELECT `word` 
                         FROM `emails1`) AND 
          `word` NOT IN (SELECT `word` 
                         FROM `emails3`);
END IF;

IF NOT EXISTS (`emails3`) THEN
            CREATE TABLE `emails3` SELECT * FROM `emails1`;
            SELECT * INTO `emails3` 
            FROM `emails2` 
            WHERE NOT `word` IN (SELECT `word` FROM `emails1`);
END IF;

TRUNCATE `emails1`;

但我一直收到这个错误,因为我是一个 mysql 菜鸟,所以我看不出有什么问题,如果你能看到代码有什么问题,请帮忙。

4

1 回答 1

1

您的查询是否存储在过程中?我不认为你可以直接运行它。我也怀疑

如果存在 ( emails3)

正在按预期工作。

您可以尝试将查询变成一个过程。

DELIMITER | 
Drop procedure if exists `backup_table`;
CREATE PROCEDURE `backup_table` () 

BEGIN 

IF EXISTS (SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'test' AND table_name = 'emails3') THEN
    INSERT INTO `emails3` SELECT * FROM `emails1`;
    INSERT INTO `emails3` SELECT * FROM `emails2` 
        WHERE NOT `word` IN (SELECT `word` FROM `emails1`) 
             AND `word` NOT IN (SELECT `word` FROM `emails3`);
END IF;

END| 

DELIMITER ;
于 2012-07-03T02:08:48.977 回答