2015-02-12 83 views
0

我可以將某些內容複製到另一個表格並在同一個表格中進行更新嗎?Mysql/PHP更新表格+插入其他表格

像:

INSERT INTO 'table_new' (name) values ("thomas") 

同時:

UPDATE 'table_old' set ChangesWereMadeAt = (the date, where the changes were made) 

我可以把其他桌上的東西,同時它還停留在舊錶,只是更新一列?

我用PHP的工作/ MySQL的

回答

1

使用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'; 
0

通過插入新行到表開始。

然後刪除第一個表中的行。

最後複製你這個插入的行:

INSERT INTO first_table 
SELECT * 
FROM dues 
WHERE id = 5; 
0

像:INSERT INTO 'table_new'(名稱)值( 「托馬斯」)

同時:

UPDATE'table_old'set 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);