从mysql中的另一个表列更新特定列

时间:2018-03-13 09:36:33

标签: mysql sql database

coaching_class table

 Field           | Type             | Null | Key | Default           | Extra                       |
+-----------------+------------------+------+-----+-------------------+-----------------------------+
| id              | int(10) unsigned | NO   | PRI | NULL              | auto_increment              |
| name            | varchar(100)     | NO   |     | NULL              |                             |
| organization_id | int(10) unsigned | NO   | MUL | NULL              |                             |
| arena_id        | int(10) unsigned | NO   | MUL | NULL              |                             |
| trainer_id      | int(10) unsigned | YES  | MUL | NULL              |                             |
| type            | varchar(1)       | NO   |     | NULL              |                             |
| sports          | int(11)          | NO   |     | NULL              |                             |
| gender          | varchar(1)       | YES  |     | NULL              |                             |
| max_entries     | int(11)          | YES  |     | NULL              |                             |
| from_date       | date             | NO   |     | NULL              |                             |
| to_date         | date             | YES  |     | NULL              |                             |
| from_time       | time             | YES  |     | NULL              |                             |
| to_time         | time             | YES  |     | NULL              |                             |
| day_slot        | int(11)          | YES  |     | NULL              |                             |
| fees            | double           | NO   |     | NULL              |                             |
| bill_cycle      | tinyint(4)       | NO   |     | 0                 |                             |
| due_after       | tinyint(4)       | YES  |     | 0                 |                             |
| created_at      | datetime         | NO   |     | CURRENT_TIMESTAMP |                             |
| updated_at      | datetime         | YES  |     | NULL              | on update CURRENT_TIMESTAMP |
| deleted_at      | datetime         | YES  |     | NULL              |  

coaching_class_entries表

| Field             | Type             | Null | Key | Default           | Extra                       |
+-------------------+------------------+------+-----+-------------------+-----------------------------+
| id                | int(10) unsigned | NO   | PRI | NULL              | auto_increment              |
| user_id           | int(10) unsigned | NO   | MUL | NULL              |                             |
| coaching_class_id | int(10) unsigned | NO   | MUL | NULL              |                             |
| fees              | double           | NO   |     | NULL              |                             |
| bill_cycle        | int(4)           | NO   |     | 0                 |                             |
| from_date         | date             | NO   |     | NULL              |                             |
| to_date           | date             | YES  |     | NULL              |                             |
| tax_group_id      | int(10) unsigned | YES  | MUL | NULL              |                             |
| last_bill_date    | date             | YES  |     | NULL              |                             |
| next_bill_date    | date             | YES  |     | NULL              |                             |
| comments          | varchar(100)     | YES  |     | NULL              |                             |
| created_at        | datetime         | NO   |     | CURRENT_TIMESTAMP |                             |
| updated_at        | datetime         | YES  |     | NULL              | on update CURRENT_TIMESTAMP |
| deleted_at        | datetime         | YES  |     | NULL              |                             |
+-------------------+------------------+------+-----+-------------------+-----

我在bill_cycle表中添加了一个新列Coaching_class_entries,并且在另一个名为bill_cycle的表中有相同的列Coaching_classes。现在,我想将bill_cycle的值从Coaching_classes复制到Coaching_class_entries bill_cycle列,以便coaching_class_entries的所有行。我是新手数据库,任何人都可以给我一些提示。

1 个答案:

答案 0 :(得分:2)

我认为一个简单的更新连接应该在这里工作:

UPDATE Coaching_class_entries t1
INNER JOIN Coaching_classes t2
    ON t1.coaching_class_id = t2.id
SET t1.bill_cycle = t2.bill_cycle;