简体   繁体   English

MySQL 查询 INSERT / SELECT / UPDATE 最后一行

[英]MySQL Query INSERT / SELECT / UPDATE last Row

i've 3 MySQL querys in a HTML POST form.我在 HTML POST 表单中有 3 个 MySQL 查询。 I want to update the last insert ROW i've inserted with my PHP script.我想用我的 PHP 脚本更新我插入的最后一个插入行。

The problem what I've is, it just updates everytime the first row instead the last row i've inserted.我遇到的问题是,它只是在每次第一行而不是我插入的最后一行时更新。

This is my query currently这是我目前的查询

INSERT INTO 
    tbl_orders (name,ordered,number,idcard,account) 
    VALUES 
    (:name,:carID,:number,:idcard,:account);
            
INSERT INTO 
    tbl_money (gesamtWert) 
    SELECT SUM(amount*price) total 
    FROM tbl_prices, tbl_car_pos 
    WHERE cid = :carID AND priceID = productID;

UPDATE 
    tbl_money 
    SET tbl_money.orderID = (SELECT tbl_orders.orderID FROM tbl_orders ORDER BY tbl_orders.orderID DESC LIMIT 1) 
    ORDER BY 
        orderID 
    DESC LIMIT 1;

First query selects the values of the form action and insert it into the first table.第一个查询选择表单操作的值并将其插入到第一个表中。

Second one is SUM the total price of the car and insert it into the second table.第二个是对汽车的总价格求和并将其插入到第二个表中。

Third one should now select the last insert row of the first query and update the last insert row from the second query.第三个现在应该 select 第一个查询的最后一个插入行,并从第二个查询更新最后一个插入行。 but as I said it updates the first row.但正如我所说,它更新了第一行。

Lets say i've inserted the last row with ID: 10 and insert now a new row with ID: 11, the third query will update the ID: 1 instead of ID:11.假设我已插入 ID 为 10 的最后一行,现在插入 ID 为 11 的新行,第三个查询将更新 ID:1 而不是 ID:11。 So if anyone can tell me where my problem is would be amazing.因此,如果有人能告诉我我的问题出在哪里,那就太棒了。

Integrate 3rd query into 2nd.将第三个查询集成到第二个。

Without optimization, using direct insert:不优化,使用直接插入:

INSERT INTO 
    tbl_orders (name,ordered,number,idcard,account) 
    VALUES 
    (:name,:carID,:number,:idcard,:account);

INSERT INTO 
    tbl_money (gesamtWert, orderID) 
    SELECT SUM(amount*price), 
           (SELECT tbl_orders.orderID FROM tbl_orders ORDER BY tbl_orders.orderID DESC LIMIT 1)
    FROM tbl_prices, tbl_car_pos 
    WHERE cid = :carID AND priceID = productID;

If you use PHP, there is a function that returns the last inserted ID.如果您使用 PHP,则有一个 function 返回最后插入的 ID。

https://www.php.net/manual/ro/function.mysql-insert-id.php https://www.php.net/manual/ro/function.mysql-insert-id.php

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

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