首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用另一个具有中间表的数据库中的另一个表的列中的部分数据更新列

使用另一个具有中间表的数据库中的另一个表的列中的部分数据更新列
EN

Stack Overflow用户
提问于 2017-07-21 08:18:37
回答 1查看 708关注 0票数 1

我在数据库中有一个表Tab1

代码语言:javascript
复制
col1    | col2
--------------
'abc-1' | 11
'abc-2' | 22
'abc-3' | 33
null    | 44
null    | 55

我希望使用来自另一个数据库( col1 )中的另一个表(Tab2)的列col3的数据更新该表中的列:

代码语言:javascript
复制
col3    | col4 | col5
---------------------
'abc-1' | 1    | 10
'abc-1' | 2    | 10
'abc-2' | 1    | 20
'abc-3' | 1    | 30
'abc-3' | 2    | 30
'abc-3' | 3    | 30
'abc-4' | 1    | 40
'abc-5' | 2    | 60

( col1中的数据总是来自col3。)

这些表通过两个中间表连接: DB1.Tab3。

代码语言:javascript
复制
col6      | col7
----------------
'abc-001' | 11
'abc-002' | 22
'abc-003' | 33
'abc-004' | 44

DB2.Tab4

代码语言:javascript
复制
col8 | col9
----------------
10   | 'abc-001'
20   | 'abc-002'
30   | 'abc-003'
40   | 'abc-004'
50   | 'abc-005'

现在,col3值可能会重复(同时由id值标识),这是一个棘手的部分。假设col1中缺少的所有值都不会在col3中重复,那么我将如何更新该列:

代码语言:javascript
复制
update DB1.Tab1 as T1
        inner join
    DB1.Tab3 as T3 ON T3.col7 = T1.col2
        inner join
    DB2.Tab4 as T4 ON T4.col9 = T3.col6
        inner join
    DB2.Tab2 as T2 ON T2.col5 = T4.col8 
set 
    T1.col1 = T2.col3
where
    T1.col1 is null;

这在一般情况下也适用于重复值--但我只想在col1值不重复时更新col3,也就是说,在这种情况下,使用的值是abc-2、abc-4、abc-5。以下是我选择单个col3值(与更新相关)的方式:

代码语言:javascript
复制
select 
    col3
from
    DB2.Tab2 as T2
        inner join
    DB2.Tab4 as T4 ON T2.col5 = T4.col8
        inner join
    DB1.Tab3 as T3 ON T4.col9 = T3.col6
        inner join
    DB1.Tab1 as T1 ON T3.col7 = T1.col2
where
    T1.col1 is null
        and T1.col2 is not null
group by col3
having count(*) = 1;

问题是:如何使用不重复的col1值使用col3更新col3?

编辑。这几乎是可行的:

代码语言:javascript
复制
update DB1.Tab1 as T1,
    (select 
        col3
    from
        DB2.Tab2 as T2
    inner join DB2.Tab4 as T4 ON T2.col5 = T4.col8
    inner join DB1.Tab3 as T3 ON T4.col9 = T3.col6
    inner join DB1.Tab1 as T1 ON T3.col7 = T1.col2
    where
        T1.col1 is null
            and T1.col2 is not null
    group by col3
    having count(*) = 1) as T2d 
set 
    T1.col1 = T2d.col3
where
    T1.col1 is null;

但是它只用一个col3值更新所有空的col3值--第一个值是select查询产生的。我认为在where条款中遗漏了一些东西,但我不能提出一个适当的条件。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-25 08:26:43

我已经找到了解决办法。这个问题相当复杂,但经过深思熟虑后,答案却很简单。

我的update语句几乎有效,但在select语句和where子句中缺少一个附加条件。

代码语言:javascript
复制
update DB1.Tab1 as T1,
    (select 
        col3, T1.col2 as T1c2
    from
        DB2.Tab2 as T2
    inner join DB2.Tab4 as T4 ON T2.col5 = T4.col8
    inner join DB1.Tab3 as T3 ON T4.col9 = T3.col6
    inner join DB1.Tab1 as T1 ON T3.col7 = T1.col2
    where
        T1.col1 is null
            and T1.col2 is not null
    group by col3
    having count(*) = 1) as T2d 
set 
    T1.col1 = T2d.col3
where
    T1.col1 is null
        and T1.col2 = T1c2;

解决方案可以归结为从要更新的表( T1.col2 )中选择另一列,特别是要更新T1.col1的值,然后将每个T1.col2与先前选择的值进行比较。

然而,我并不清楚它背后的机制,特别是为什么没有此编辑的update语句将只更新所有字段,只有一个值,所以注释仍然很受欢迎。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45232591

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档