Sql 在通过引用表将数据移动到其他表时替换列的值
让我们假设我有一个名为“Table1”的表,其列和值如下所示Sql 在通过引用表将数据移动到其他表时替换列的值,sql,hive,hiveql,Sql,Hive,Hiveql,让我们假设我有一个名为“Table1”的表,其列和值如下所示 --------------------------- action | component | type | --------------------------- 1 | 2 | 1 | 2 | 3 | 3 | 3 | 4 | 2 | --------------------------- “表2”的结构与“表1”完全相同 现在我有另
---------------------------
action | component | type |
---------------------------
1 | 2 | 1 |
2 | 3 | 3 |
3 | 4 | 2 |
---------------------------
“表2”的结构与“表1”完全相同
现在我有另一个表“参考”,如下所示
---------------------------
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”表中的值,将数据从“table1”移动到“table2”。我的结果表应该如下所示
请帮我查询同样的问题。提前感谢。您正在寻找多个联接:
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
您使用的是哪种SQL?在发布之前你们有并没有尝试过任何东西?实际上我正在尝试这是配置单元查询,我尝试的是在选择时,我尝试用select语句替换特定的列,该语句包括join语句和引用表。但它没有发生。也许我的问题不清楚?实际上,我们需要通过引用“reference”表中的特定值,将数据从“table1”填充到“table2”。在您的回答中,您没有引用“reference”表,对吗?@CodingOwl只是名称错了。查一下最新消息,兄弟,你救了我一天。我想在hive中进行查询,这非常有效。
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