【问题标题】:How to use Insert query with multiple tables at a same time?如何同时对多个表使用插入查询?
【发布时间】:2017-04-20 15:33:18
【问题描述】:

我有一个表格,比如说账单表格。我有 3 张桌子。

  1. bill (billId->主键)
  2. billdetails(billdetailId->主键,billId->外键)
  3. fintran(finalId -> 主键,form一共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;

插入数据后,我想要billId,即billdetailsfintran 表中账单表的主键。但是通过这个查询,我只能在billdetail 表中得到它。在fintran 表中,我得到billdetail 表的主键。请帮助我。

【问题讨论】:

标签: php mysql insert last-insert-id


【解决方案1】:

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

检查这个:MySQL Insert into multiple tables? (Database normalization?)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-11
    • 1970-01-01
    • 2011-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多