开发者

mysql move row between tables [duplicate]

开发者 https://www.devze.com 2023-02-19 17:26 出处:网络
This question already has answers here: mysql - move rows from one table to another (8 answers) Closed last year.
This question already has answers here: mysql - move rows from one table to another (8 answers) Closed last year.

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 usi开发者_开发百科ng php. My current plan is to select from table1, store data in php, insert into table2, then delete from table1. This seems like a very long process.

Is this really the best way to do this?


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


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


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.


Just make it one table and make a flag field to determine if record is moderated or not.

So, the only thing you will need is just simple update query.

That's the way such things being done in general.


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.)


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;


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

精彩评论

暂无评论...
验证码 换一张
取 消