Is it possible to move a record from one table to another using a single SQL statement?

后端 未结 5 1692
我寻月下人不归
我寻月下人不归 2021-01-19 10:41

I need a query to move a record from one table to another without using multiple statements?

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-19 11:17

    There is no way to make it a single query, but if you HAVE to do it in a single query within an application you can create a Stored Procedure to do it for you.

    DELIMITER $$
    
    DROP PROCEDURE IF EXISTS `copydelete` $$
    CREATE PROCEDURE `copydelete` (id INT)
    BEGIN
    INSERT INTO New_Table SELECT * from Old_Table WHERE Old_Table.IdField=id;
    DELETE FROM Old_Table where IdField=id;
    END $$
    
    DELIMITER ;
    

    Then you're new query is just

    CALL copydelete(4);
    

    Which will delete WHERE IdField=4;

提交回复
热议问题