【问题标题】:Query to update and delete records in two different tables查询更新和删除两个不同表中的记录
【发布时间】:2018-02-19 01:03:09
【问题描述】:

我想编写一个查询,从表中删除记录并更新另一个记录。这是我的查询:

DELETE FROM borrowed_books a WHERE a.id = '$id'  
            AND
             UPDATE books b SET b.nr_copies=b.nr_copies+1 where 
b.id_book=a.id_book 

控制台中的错误说: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version

我正在使用 mySQL 和 XAMPP。

【问题讨论】:

    标签: mysql xampp mariadb


    【解决方案1】:

    你不能在一个查询中做两个动作

    你必须使用两个查询

    DELETE FROM borrowed_books a WHERE a.id = '$id' 
    ; 
    
    UPDATE books b 
    INNER JOIN borrowed_books a 
    SET b.nr_copies=b.nr_copies+1 
    where b.id_book=a.id_book 
    and a.id = '$id' 
    ;
    

    使用更新加入

    最终你可以在一个命令中检查你的 mysql 驱动程序是否有多个查询

    【讨论】:

    • 将它们放入交易中!
    猜你喜欢
    • 2016-05-20
    • 2019-11-10
    • 1970-01-01
    • 1970-01-01
    • 2011-11-27
    • 2015-07-12
    • 2016-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多