簡體   English   中英

如何同時對多個表使用插入查詢?

[英]How to use Insert query with multiple tables at a same time?

我有一種表格,例如帳單表格。 我有3張桌子。

  1. 帳單(billId->主鍵)
  2. billdetails(billdetailId->主鍵,billId-> foregin鍵)
  3. fintran(finalId->主鍵,總共有10個輸入形式。

提交時,前5個輸入應進入帳單表,其他5個輸入應進入帳單明細。 所有這些都將進入決賽桌。 我用下面的查詢。

BEGIN;
$sql = mysqli_query($conn,"INSERT INTO `bill`(`societyId`, `unitId`, `memberId`, `area`, `arrear`, `invoiceNumber`, `invoiceDate`, `dueDate`, `billingPeriod`, `total`, `interestOnArrear`, `totalDue`, `email`, `penalty`, `principalArrears`, `interestArrears`, `advancedAdjusted`, `netPending`) VALUES ('".$societyId."','".$unitId."','".$memberId."','".$area."','".$arrear."','".$invoiceNumber."','".$invoiceDate."','".$dueDate."','".$billingPeriod."','".$total."','".$interestOnArrear."','".$totalDue."','".$email."','".$penalty."','".$principalArrears."','".$interestArrears."','".$advancedAdjusted."','".$netPending."')");     
$memo = $_REQUEST['memo'];
$charge = $_REQUEST['charge'];
$sql= mysqli_query($conn,"INSERT INTO `billdetail`(`biilId`, `memo`, `charge`) VALUES (LAST_INSERT_ID(),'".$memo."','".$charge."')");
$sql= mysqli_query($conn,"INSERT INTO `fintran`(`societyId`, `docId`, `docTypeName`, `docDate`, `unitId`, `unitName`, `memberId`, `memberName`, `comboId`, `ledgerId`, `ledgerName`, `debit`, `credit`, `netValue`) VALUES ('".$societyId."',LAST_INSERT_ID(),'bill','','".$unitId."','".$unitId."','".$memberId."','".$memberId."','','".$charge."','','','','')");
COMMIT;

插入數據后,我想要billIdbilldetailsfintran表中票據表的主鍵。 但是通過此查詢,我只能在billdetail表中獲取此信息。 fintran表,我得到的主鍵billdetail表。 請同樣幫我。

不,您不能在一個MySQL命令中插入多個表。 但是,您可以使用事務。

檢查一下: MySQL插入多個表? (數據庫規范化?)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM