![](/img/trans.png)
[英]MySQL: Get last update id from a table to insert it in an other table
[英]Mysql/PHP UPDATE the table + INSERT INTO an other table
我可以复制内容到另一个表中,也可以在同一张表中更新它吗?
喜欢:
INSERT INTO 'table_new' (name) values ("thomas")
与此同时:
UPDATE 'table_old' set ChangesWereMadeAt = (the date, where the changes were made)
我可以在其他表中放一些东西,而同时又保留在旧表中并只更新一列吗?
我使用PHP / MySql
使用LAST_INSERT_ID();
INSERT INTO 'table_new' (name) values ("thomas");
$last_id_inserted = LAST_INSERT_ID();
UPDATE 'table_old' SET blah='$blah' WHERE id='$last_id_inserted';
首先将新行插入表中。
然后从第一个表中删除该行。
最后复制与此插入的行:
INSERT INTO first_table
SELECT *
FROM dues
WHERE id = 5;
像:INSERT INTO'table_new'(名称)值(“托马斯”)
与此同时:
UPDATE'table_old'设置ChangesWereMadeAt =(进行更改的日期)
基于此,我想说解决方案是提出两个请求。
这意味着...我可以将某些东西放到另一个表中,同时又将其保留在旧表中并只更新一个列吗?
但是基于此,我可以提出一种解决方案。
$reqSelect= 'select * from YourTable
where [insert the condition that would make you select only the data you want to copy]';
$data = mysqli_query($connection, $reqSelect);
$reqInsert='insert into YourDestinationTable(row1, row2, etc)
values('.$data[row1].', '.$data[row2].', '.$data[etc]);
mysqli_query($connection, $reqInsert);
$reqUpdate='update YourTable where [conditions here]
set TheRowYouWantToModify = '.data[TheDataYouWantInYourRow];
mysqli_query($connection, $reqUpdate);
听起来这似乎更容易解决我的MySql触发器。 这里有一个基本示例: 创建触发器以将数据添加到审核表
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.