插入选择自删除

时间:2012-03-01 07:15:25

标签: mysql select sql-delete

我想从table1向表2传输50行,我该怎么做?

建议代码:

    INSERT INTO table2 SELECT * FROM table1 
    WHERE uid IN ('23','34','345','567','3242','34322','2') DELETE;

3 个答案:

答案 0 :(得分:4)

写2条陈述。如果您使用InnoDB,请将它们放在一个事务中:

START TRANSACTION ;

INSERT INTO table2 
  SELECT * 
  FROM table1 
  WHERE uid IN ('23','34','345','567','3242','34322','2') ;

DELETE FROM table1 
WHERE uid IN ('23','34','345','567','3242','34322','2') ;

COMMIT ;

答案 1 :(得分:0)

INSERT INTO table1 (col1, col2, col3) SELECT col1, col2, col3 FROM table2 WHERE col4='some value'

http://www.databasejournal.com/features/mssql/article.php/3507171/Transferring-Data-from-One-Table-to-Another.htm

答案 2 :(得分:0)

INSERT INTO TABLE2 SELECT * FROM TABLE1 WHERE COL1 = 'A'