天天看點

mysql 删除表資料插入到另一張表_SQL如何将将一張表的部分資料查詢後插入到另一張表并同時删除從原表查詢的資料?...

更新曆史表内容

mysql> select * from A;

+----+---------+--------+

| Id | name    | scores |

+----+---------+--------+

|  1 | xiaoyi  |     88 |

|  2 | xiaoer  |     89 |

|  3 | xiaosan |     90 |

|  4 | xiaosi  |     91 |

+----+---------+--------+

4 rows in set (0.00 sec)

mysql> select * from A_his;

+----+---------+--------+

| Id | name    | scores |

+----+---------+--------+

|  1 | xiaoyi  |     88 |

|  2 | xiaoer  |     98 |

|  3 | xiaosan |     87 |

|  4 | xiaosi  |     91 |

+----+---------+--------+

drop procedure if exists sp1;

delimiter //

create procedure sp1(a int)

begin

delete from A_his where Id = a;

insert into A_his select * from A where Id = a;

select * from A_his;

end//

delimiter ;

mysql> call sp1(2); -- 顯示更新後的A_his 表的内容

+----+---------+--------+

| Id | name    | scores |

+----+---------+--------+

|  1 | xiaoyi  |     88 |

|  2 | xiaoer  |     89 |

|  3 | xiaosan |     87 |

|  4 | xiaosi  |     91 |

+----+---------+--------+

4 rows in set (0.09 sec)