本文介紹了MySQL,一次查詢更新多張表的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我有一個更新三個表的函數,但我使用三個查詢來執行此操作.我希望使用更方便的方法進行良好的實踐.
I have a function that updates three tables, but I use three queries to perform this. I wish to use a more convenient approach for good practice.
如何使用單個查詢更新 MySQL 中的多個表?
How can I update multiple tables in MySQL with a single query?
推薦答案
以兩個表為例,Books
和 Orders
.如果我們在 Orders
表中使用 Order.ID = 1002
增加特定訂單中的書籍數量,那么我們還需要減少可用書籍的總數我們在 Books
表中的相同數量的庫存.
Take the case of two tables, Books
and Orders
. In case, we increase the number of books in a particular order with Order.ID = 1002
in Orders
table then we also need to reduce that the total number of books available in our stock by the same number in Books
table.
UPDATE Books, Orders
SET Orders.Quantity = Orders.Quantity + 2,
Books.InStock = Books.InStock - 2
WHERE
Books.BookID = Orders.BookID
AND Orders.OrderID = 1002;
這篇關于MySQL,一次查詢更新多張表的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!