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

时间:2021-09-29 17:07:18

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 table


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

I have a added a new column bill_cycle in Coaching_class_entries table and I have the same column bill_cycle in another table called Coaching_classes . Now I want to copy the values of bill_cycle from Coaching_classes to Coaching_class_entries bill_cycle column for all the rows of coaching_class_entries. I am very new to database can anyone give me some hint please.

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

1 个解决方案

#1


2  

I think a simple update join should work here:

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

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

#1


2  

I think a simple update join should work here:

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

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