Boo Yan Jiong
Boo Yan Jiong

Reputation: 2741

Update multiple table with different WHERE clause in one query

I want to combine three different UPDATE queries, in different table, with different WHERE conditions, into a single mySql query. Is it possible?

Reason: send 1 request to mySql server is faster than sending 3 requests separately :)

UPDATE client SET clientCount = clientCount + 1 WHERE clientType = 2
UPDATE storage SET soldItem = soldItem + 1 WHERE itemType = 5
UPDATE employee SET doWork = 1, totalSale = totalSale + 1 WHERE employeeId = 12

The UPDATE statements are independent, and are not related to each other. I tried to find some solution, however, the

UPDATE client, storage, employee SET client.clientCount = ... , storage.soldItem = ... WHERE ... ? ? ? ...

does not fit to my case as my three UPDATE statements are independent...

Is it possible to combine 3 independents queries into a 1 query?

Upvotes: 0

Views: 221

Answers (1)

Pons
Pons

Reputation: 1111

Create a stored procedure with all these update statements and call the stored procedure from your code.

Upvotes: 2

Related Questions