通过将一个 table 与另一个进行比较,将数据添加到 table
Adding data to table by comparing one table to another
我正在努力将这个大的 table 拆分成较小的 table 以便通过外键进行管理,并且在尝试将数据重新组合在一起时遇到了难题。我刚开始使用这种类型的数据合并,所以我有点迷茫。
有 3 个 table:一个 table 包含产品所有者列表,一个 table 包含他们负责的系统列表,原始 table 包含所有数据(示例如下):
Product Owners Table:
+----+---------------+
| id | product_owner |
+----+---------------+
| 1 | User1 |
+----+---------------+
PRIMARY KEY: id
System Table:
+----+-----------+---------------+
| id | system | product_owner |
+----+-----------+---------------+
| 6 | Server1 | NULL |
+----+-----------+---------------+
FOREIGN KEY: product_owner(id)
Original Table:
+---------+---------------+
| system | product_owner |
+---------+---------------+
| Server1 | User1 |
+---------+---------------+
我想从原来的 table 中获取数据并将其与新系统 table 合并,但是我不想走多个 UPDATE
语句的路线添加需要添加的内容。解决这个问题的最佳途径是什么?
经过更多的探索和阅读更多的术语后,我能够通过结合使用 join
语句与 update
、set
和 [ 来解决我的问题=14=] 语句:
update system_table a
join original_table.system b
on a.system = b.system
join product_owner c
on c.product_owner = b.product_owner
set a.product_owner = c.id
where c.product_owner = b.product_owner;
我正在努力将这个大的 table 拆分成较小的 table 以便通过外键进行管理,并且在尝试将数据重新组合在一起时遇到了难题。我刚开始使用这种类型的数据合并,所以我有点迷茫。
有 3 个 table:一个 table 包含产品所有者列表,一个 table 包含他们负责的系统列表,原始 table 包含所有数据(示例如下):
Product Owners Table:
+----+---------------+
| id | product_owner |
+----+---------------+
| 1 | User1 |
+----+---------------+
PRIMARY KEY: id
System Table:
+----+-----------+---------------+
| id | system | product_owner |
+----+-----------+---------------+
| 6 | Server1 | NULL |
+----+-----------+---------------+
FOREIGN KEY: product_owner(id)
Original Table:
+---------+---------------+
| system | product_owner |
+---------+---------------+
| Server1 | User1 |
+---------+---------------+
我想从原来的 table 中获取数据并将其与新系统 table 合并,但是我不想走多个 UPDATE
语句的路线添加需要添加的内容。解决这个问题的最佳途径是什么?
经过更多的探索和阅读更多的术语后,我能够通过结合使用 join
语句与 update
、set
和 [ 来解决我的问题=14=] 语句:
update system_table a
join original_table.system b
on a.system = b.system
join product_owner c
on c.product_owner = b.product_owner
set a.product_owner = c.id
where c.product_owner = b.product_owner;