从2个表中选择更新所有字段

时间:2015-04-06 17:37:28

标签: mysql

提供商:

SELECT ID,has_account from service_providers where has_account='yes';

+-----+-------------+
| ID  | has_account |
+-----+-------------+
|  17 | yes         |
| 687 | yes         |
| 158 | yes         |
| 292 | yes         |
| 306 | yes         |
| 444 | yes         |
| 536 | yes         |
| 572 | yes         |
| 601 | yes         |
| 697 | yes         |
+-----+-------------+

帐户:

SELECT ID,active from accounts;

+-----+--------+
| ID  | active |
+-----+--------+
|   1 | yes    |
|   3 | yes    |
|   4 | yes    |
|   7 | yes    |
|   8 | yes    |
|   9 | yes    |
|  10 | yes    |
|  11 | yes    |
|  12 | yes    |
|  13 | no     |
|  14 | yes    |
|  15 | yes    |
|  16 | no     |
+-----+--------+

我想在mysql中使用它:

if account == active
set provider.has_account == 'yes'

我需要将has_account字段设置为yes id帐户是帐户处于活动状态。

为了保护隐私,我们可以说表格是通过ID

连接的

1 个答案:

答案 0 :(得分:1)

加入表并使用UPDATE语句设置字段:

UPDATE providers AS p
JOIN accounts AS a ON p.id = a.id
SET p.has_account = 'yes'
WHERE a.active = 'yes'