简体   繁体   English

将值从一个表插入到另一个表中,同时进行两次插入

[英]insert value from one table into another whilst doing a double insert

Not sure that this is even possible. 不知道这是否有可能。 I am inserting values into two tables at the same time using multi_query. 我正在使用multi_query将值同时插入两个表中。 That works fine. 很好 One of the tables has an auto increment column and I need to take the last auto incremented number and insert it into the second table so like this: insert into table 1 then take the last inserted number from column X and insert it along with other data into table 2. I have played around with using SELECT LAST and INSERT INTO but so far its just doing my head in. The insert statements looks like this: 其中一个表具有一个自动递增列,我需要获取最后一个自动递增的数字并将其插入到第二个表中,如下所示:插入表1中,然后从X列获取最后插入的数字并将其与其他数据一起插入进入表2。我已经使用SELECT LAST和INSERT INTO进行了尝试,但到目前为止,它只是在起作用。insert语句如下所示:

$sql= "INSERT INTO tbleClients (riskClientId, riskFacility, riskAudDate)     VALUES ('$riskclient2', '$facility2', '$riskdate2');";

$sql .="SELECT     LAST(riskId) FROM tbleClients;";$sql .="INSERT INTO tblRiskRegister (riskId)     SELECT riskId FROM tbleClients ;";

$sql .= "INSERT INTO tblRiskRegister     (riskAudDate, riskClientId,       riskSessionId, RiskNewId) VALUES     ('$riskdate2', '$riskclient2', '$sessionid', '$risknewid')";

Individually they all produce results but I need it to happen simultaneously. 个别而言,它们都会产生结果,但是我需要同时发生。 I did toy with the idea of doing them all separately but figure thats not very efficient. 我做玩具的想法是将它们全部分开做,但认为那不是很有效。 Any pointers would be appreciated. 任何指针将不胜感激。

$sql= "INSERT INTO tbleClients (riskClientId, riskFacility, riskAudDate)     VALUES ('$riskclient2', '$facility2', '$riskdate2');";

After executing the above query, using mysqli_insert_id(), which gives you the last insert id. 执行上述查询后,使用mysqli_insert_id()提供最后的插入ID。

So below query is useless. 所以下面的查询是没有用的。 $sql .="SELECT LAST(riskId) FROM tbleClients;"; $ sql。=“从tbleClients中选择LAST(riskId);”;

$sql .="INSERT INTO tblRiskRegister (riskId)     SELECT riskId FROM tbleClients ;";

You can insert last_insert_id in above query. 您可以在上述查询中插入last_insert_id。

Unable to find the relation between above & below query. 无法找到上下查询之间的关系。

$sql .= "INSERT INTO tblRiskRegister     (riskAudDate, riskClientId,       riskSessionId, RiskNewId) VALUES     ('$riskdate2', '$riskclient2', '$sessionid', '$risknewid')";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM