通过引用 table 将数据移动到其他 table 时替换列的值
To replace value of a column while moving data to other table by referring reference table
假设我有一个名为 'Table1' 的 table,其列和值如下
---------------------------
action | component | type |
---------------------------
1 | 2 | 1 |
2 | 3 | 3 |
3 | 4 | 2 |
---------------------------
和 'Table2' 与 'table1'.
具有完全相同的结构
现在我有另一个 table 'reference' 如下
---------------------------
description | id | value |
---------------------------
action | 2 | create|
action | 1 | delete|
action | 3 | update|
component | 2 | c1 |
component | 4 | c2 |
component | 3 | c3 |
type | 2 | t1 |
type | 1 | t2 |
type | 3 | t3 |
---------------------------
现在,我需要通过引用 'reference' table 中的值将数据从 'table1' 移动到 'table2'。我的结果 table 应该如下所示。
action | component | type |
---------------------------
delete | c1 | t2 |
create | c3 | t3 |
update | c2 | t1 |
---------------------------
请帮我查询一下。提前致谢。
您正在寻找多个连接:
select t1a.value as action,
t1c.value as component,
t1t.value as type
from table2 t2 join
table1 t1a
on t2.action = t1a.id and t1a.description = 'action' join
table1 t1c
on t2.component = t1c.id and t1c.description = 'component' join
table1 t1t
on t2.type = t1t.id and t1t.description = 'type';
你可以这样做:
select
(select b.description from reference b where A.action = b.id and b.description = 'action') as action,
(select c.description from reference c where A.action = c.id and c.description = 'component') as component,
(select d.description from reference d where A.action = d.id and d.description = 'type') as type
from table_1 A
假设我有一个名为 'Table1' 的 table,其列和值如下
---------------------------
action | component | type |
---------------------------
1 | 2 | 1 |
2 | 3 | 3 |
3 | 4 | 2 |
---------------------------
和 'Table2' 与 'table1'.
具有完全相同的结构现在我有另一个 table 'reference' 如下
---------------------------
description | id | value |
---------------------------
action | 2 | create|
action | 1 | delete|
action | 3 | update|
component | 2 | c1 |
component | 4 | c2 |
component | 3 | c3 |
type | 2 | t1 |
type | 1 | t2 |
type | 3 | t3 |
---------------------------
现在,我需要通过引用 'reference' table 中的值将数据从 'table1' 移动到 'table2'。我的结果 table 应该如下所示。
action | component | type |
---------------------------
delete | c1 | t2 |
create | c3 | t3 |
update | c2 | t1 |
---------------------------
请帮我查询一下。提前致谢。
您正在寻找多个连接:
select t1a.value as action,
t1c.value as component,
t1t.value as type
from table2 t2 join
table1 t1a
on t2.action = t1a.id and t1a.description = 'action' join
table1 t1c
on t2.component = t1c.id and t1c.description = 'component' join
table1 t1t
on t2.type = t1t.id and t1t.description = 'type';
你可以这样做:
select
(select b.description from reference b where A.action = b.id and b.description = 'action') as action,
(select c.description from reference c where A.action = c.id and c.description = 'component') as component,
(select d.description from reference d where A.action = d.id and d.description = 'type') as type
from table_1 A