I have two tables like this:
// users
+----+--------+----------------------------+------------------+
| id | name | email | notification_num |
+----+--------+----------------------------+------------------+
| 1 | John | John134@gmail.com | 4 |
| 2 | Peter | matrix_peter@ymail.com | 2 |
| 3 | Jack | jk_43m@gmail.com | 6 |
+----+--------+----------------------------+------------------+
// notifications
+----+---------+---------+------+
| id | post_id | user_id | seen |
+----+---------+---------+------+
| 1 | 4325 | 2 | NULL |
| 2 | 8765 | 3 | NULL |
| 3 | 5454 | 3 | 1 |
| 4 | 4346 | 2 | NULL |
| 5 | 3243 | 1 | 1 |
| 6 | 3246 | 2 | 1 |
| 7 | 3764 | 3 | 1 |
+----+---------+---------+------+
Ok, All I have is just a user_id. For example :user_id = 2
, And then I want this output:
// users
+----+--------+----------------------------+------------------+
| id | name | email | notification_num |
+----+--------+----------------------------+------------------+
| 1 | John | John134@gmail.com | 4 |
| 2 | Peter | matrix_peter@ymail.com | 0 |
| 3 | Jack | jk_43m@gmail.com | 6 |
+----+--------+----------------------------+------------------+
// notifications
+----+---------+---------+------+
| id | post_id | user_id | seen |
+----+---------+---------+------+
| 1 | 4325 | 2 | 1 |
| 2 | 8765 | 3 | NULL |
| 3 | 5454 | 3 | 1 |
| 4 | 4346 | 2 | 1 |
| 5 | 3243 | 1 | 1 |
| 6 | 3246 | 2 | 1 |
| 7 | 3764 | 3 | 1 |
+----+---------+---------+------+
As you see, I want to update users.cotification_num
and set it to 0
(for such an user), and I also want to update notifications.seen
and set all rows to 1
(for such an user). How can I do that?
I can do that by two different queries like this:
UPDATE users SET notification_num = 0 WHERE id = :user_id
UPDATE notifications SET seen = 1 WHERE seen is NULL and user_id = :user_id
But I want to know how can I do that by one query?