如何在一个查询中更新两个不同的表?

时间:2016-08-29 22:58:10

标签: mysql sql sql-update

我有两张这样的表:

// 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    |
+----+---------+---------+------+

好的,我只拥有 user_id 。例如:user_id = 2,然后我想要这个输出:

// 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    |
+----+---------+---------+------+

如您所见,我想更新users.cotification_num并将其设置为0 (对于此类用户),我还想更新notifications.seen并将所有行设置为1 (对于此类用户)。我怎么能这样做?

我可以通过两个不同的查询来做到这一点:

UPDATE users SET notification_num = 0 WHERE id = :user_id

UPDATE notifications SET seen = 1 WHERE seen is NULL and user_id = :user_id

但我想知道如何通过一个查询来做到这一点?

3 个答案:

答案 0 :(得分:2)

您可以使用多表UPDATE

UPDATE users u LEFT JOIN notifications n ON n.user_id = u.id
SET    u.notification_num = 0,
       n.seen = 1
WHERE  u.id = 2

答案 1 :(得分:2)

您可以在一个查询中将这些表一起加入UPDATE

UPDATE users, notifications
SET users.notification_num = 0
  ,  notification.seen = 1
WHERE users.id = notifications.user_id AND
      users.id = 2 AND
      notifications.seen is NULL

阅读here了解详情。另一种选择是在事务中简单地进行2次单独的更新:

START TRANSACTION;
UPDATE users SET notification_num = 0 WHERE id = 2;
UPDATE notifications SET seen = 1 WHERE user_id = 2 AND notifications.seen is NULL;
COMMIT;

答案 2 :(得分:1)

请注意,我的架构与您的架构略有不同 - 但它的差别很小......

 DROP TABLE IF EXISTS users;

CREATE TABLE users 
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,name  VARCHAR(12) NOT NULL
,email VARCHAR(20) NOT NULL
,notification_num INT NOT NULL
);

INSERT INTO users VALUES
(1,'John','John134@gmail.com',4),
(2,'Peter','matrix_peter@ymail.com',2),
(3,'Jack','jk_43m@gmail.com',6);

DROP TABLE IF EXISTS notifications;

CREATE TABLE notifications 
(post_id INT NOT NULL
,user_id INT NOT NULL
,seen TINYINT DEFAULT 0
,PRIMARY KEY(post_id,user_id)
);

INSERT INTO notifications VALUES
(4325,2,0),
(8765,3,0),
(5454,3,1),
(4346,2,0),
(3243,1,1),
(3246,2,1),
(3764,3,1);

 SELECT * FROM notifications;
 +---------+---------+------+
 | post_id | user_id | seen |
 +---------+---------+------+
 |    3243 |       1 |    1 |
 |    3246 |       2 |    1 |
 |    3764 |       3 |    1 |
 |    4325 |       2 |    0 |
 |    4346 |       2 |    0 |
 |    5454 |       3 |    1 |
 |    8765 |       3 |    0 |
 +---------+---------+------+

 SELECT * FROM users;
 +----+-------+----------------------+------------------+
 | id | name  | email                | notification_num |
 +----+-------+----------------------+------------------+
 |  1 | John  | John134@gmail.com    |                4 |
 |  2 | Peter | matrix_peter@ymail.c |                2 |
 |  3 | Jack  | jk_43m@gmail.com     |                6 |
 +----+-------+----------------------+------------------+


UPDATE users u 
  JOIN notifications n 
    ON n.user_id = u.id
   SET u.notification_num = 0, n.seen = 1
 WHERE u.id = 2;

 SELECT * FROM users;
  +----+-------+----------------------+------------------+
  | id | name  | email                | notification_num |
  +----+-------+----------------------+------------------+
  |  1 | John  | John134@gmail.com    |                4 |
  |  2 | Peter | matrix_peter@ymail.c |                0 |
  |  3 | Jack  | jk_43m@gmail.com     |                6 |
  +----+-------+----------------------+------------------+

  SELECT * FROM notifications;
  +---------+---------+------+
  | post_id | user_id | seen |
  +---------+---------+------+
  |    3243 |       1 |    1 |
  |    3246 |       2 |    1 |
  |    3764 |       3 |    1 |
  |    4325 |       2 |    1 |
  |    4346 |       2 |    1 |
  |    5454 |       3 |    1 |
  |    8765 |       3 |    0 |
  +---------+---------+------+