mysql move row between tables

后端 未结 7 944
后悔当初
后悔当初 2020-12-14 19:06

I have 2 tables, table1 and table2.

I have a row in table1 and want to move it to table2 and remove it from table1. Basically, a cut+paste.

I am using php.

相关标签:
7条回答
  • 2020-12-14 19:23

    I had to solve the same issue and this is what I used as solution.

    To use this solution the source and destination table must be identical, and the must have an id unique and auto-increment in first table (so that the same id is never reused).

    Lets say table1 and table2 have this structure

    |id|field1|field2

    You can make those two queries:

    INSERT INTO table2
    SELECT *
    FROM table1
    WHERE <your_where_clause>
    
    DELETE FROM table1 
    WHERE table1.id in
    (SELECT table2.id 
    FROM table2)
    

    This seems to me to be a better solution then the one suggested by Pieter888, because that solution doesn't take into account that data selected can be different than the data deleted (because data in table 1 can change between first and second query), while this way you are safe.

    0 讨论(0)
  • 2020-12-14 19:26

    Use INSERT INTO ... SELECT to insert data from another table

    0 讨论(0)
  • 2020-12-14 19:34

    I found that I had to prep the data between the initial query and the insert. This was on a codeigniter-based CMS, so it uses their functions and return formatting, but may help someone.

    function move_row($id = 0) {
        /* first copy it from table_one to table_two */
        $query = $this->db->query('select * from table_one where id = ' . $id);
        $row = $query->row_array(); /* this appears to be required */
        $this->db->insert('table_two',$row);
    
        /* then delete it from table_one */
        return($this->db->query('delete from table_one where id = ' . $id));
    }
    

    (Bare-bones, no error-checking, etc.)

    0 讨论(0)
  • 2020-12-14 19:35

    You're going to need at least 2 queries:

    INSERT INTO table2 (column_name1, column_name2) SELECT column_name1, column_name2 FROM table 1 WHERE <insert_where_clause_here>
    
    DELETE FROM table1 WHERE <your_where_clause>
    

    I see no shorter way of doing this using MySQL

    0 讨论(0)
  • 2020-12-14 19:35

    The short answer to the question as asked, is as a lot of people already answered, no.

    But what I sense you're looking for is a way to insert into the new table and remove from the old in one operation. This is what transactions are for.

    The changes won't be affected until the transaction is commited which will have the same effect as if the rows where actually moved.

    (Not moved internally in the MySQL engine but for users/queries)

    I would use INSERT SELECT and wrap the commands in a TRANSACTION something like this:

    DECLARE EXIT HANDLER FOR SQLEXCEPTION, SQLWARNING, NOT FOUND
    BEGIN 
      ROLLBACK; 
      CALL ERROR_ROLLBACK_OCCURRED; 
    END;
    
    START TRANSACTION;
    INSERT INTO table_new SELECT a,b,c FROM table_old WHERE x = 'something';
    DELETE FROM table_old WHERE x = 'something';
    COMMIT;
    
    0 讨论(0)
  • You can use multiple query one at a time like this

    On the command button that has the function of committing your data on table 1

    Use this:

    $query = mysql_query(insert into table1)
    

    Instead of this:

    mysql_query(insert into table2)
    

    And under the query that insert into table 1 write this:

    $query2 = mysql_query(INSERT into TABLE2)
    TRUNCATE TABLE TABLE2;
    

    It gives you the privileges to perform two queries one at a time

    0 讨论(0)
提交回复
热议问题