MySQL, update multiple tables with one query

前端 未结 6 1780
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 08:00

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.

How can I update mu

6条回答
  •  春和景丽
    2020-11-22 08:31

    Let's say I have Table1 with primary key _id and a boolean column doc_availability; Table2 with foreign key _id and DateTime column last_update and I want to change the availability of a document with _id 14 in Table1 to 0 i.e unavailable and update Table2 with the timestamp when the document was last updated. The following query would do the task:

    UPDATE Table1, Table2 
    SET doc_availability = 0, last_update = NOW() 
    WHERE Table1._id = Table2._id AND Table1._id = 14
    

提交回复
热议问题