Search code examples
mysqlmysql-workbenchmysql-error-1064

MYSQL query to update data in three tables


I have following three tables

Table1

+------+--------+
| GID  | Active |
+------+--------+
| 110  |  Yes   |
+------+--------+
GID is primary key

Table2

+------+--------+
| UID  | Active |
+------+--------+
| 110  |  Yes   |
| 110  |  Yes   |
+------+--------+

Table3

+------+--------+
| FID  | Active |
+------+--------+
| 110  |  Yes   |
| 110  |  Yes   |
+------+--------+

I want to update 3 tables with all the value of field Active to 'no' , how can I achieve this ? Your suggestions are highly appreciated, I hope the given information is subsequent to make a conclusion on this ?

Additional information :- Following is the query I am executing in table1 now.

  $id = 110;
  UPDATE table1 SET active = 'no' WHERE gid = {$id}"

Solution

  • This is how to update all rows with given id:

    UPDATE 
        Table1, Table2, Table3
    SET 
        Table1.Active = 'No', 
        Table2.Active = 'No', 
        Table3.Active = 'No'
    WHERE
        Table1.GID = Table2.UID
    AND Table2.UID = Table3.FID
    AND Table3.FID = 110;