有没有办法在DB2的update语句中使用连接?
谷歌真的让我失望了这大致是我想要实现的目标(......除了明显的工作......)
update file1 inner join file2
on substr(file1.firstfield,10,20) = substr(file2.anotherfield,1,10)
set file1.firstfield = ( 'BIT OF TEXT' concat file2.something )
where file1.firstfield like 'BLAH%'
干杯
答案 0 :(得分:8)
您没有说明您要定位的平台。但是,将表作为文件引用,使我相信您不是在Linux,UNIX或Windows(LUW)上运行DB2。
但是,如果您在DB2 LUW上 ,请参阅MERGE声明:
对于您的示例语句,这将写为:
merge into file1 a
using (select anotherfield, something from file2) b
on substr(a.firstfield,10,20) = substr(b.anotherfield,1,10)
when matched and a.firstfield like 'BLAH%'
then update set a.firstfield = 'BIT OF TEXT' || b.something;
请注意:对于DB2,SUBSTR函数的第三个参数是要返回的字节数,而不是结束位置。因此,SUBSTR(a.firstfield,10,20)返回CHAR(20)。但是,SUBSTR(b.anotherfield,1,10)返回CHAR(10)。我不确定这是否是故意的,但可能会影响你的比较。
答案 1 :(得分:7)
update
语句中的联接是非标准的,并非所有供应商都支持。您尝试做的事情可以通过子选择完成:
update
file1
set
firstfield = (select 'stuff' concat something from file2 where substr(file1.field1, 10, 20) = substr(file2.xxx,1,10) )
where
file1.foo like 'BLAH%'
答案 2 :(得分:6)
试试这个,然后告诉我结果:
UPDATE File1 AS B
SET b.campo1 = (SELECT DISTINCT A.campo1
FROM File2 A
INNER JOIN File1
ON A.campo2 = File1.campo2
AND A.campo2 = B.campo2)
答案 3 :(得分:3)
这是我刚刚开始工作的一个很好的例子:
update cac c
set ga_meth_id = (
select cim.ga_meth_id
from cci ci, ccim cim
where ci.cus_id_key_n = cim.cus_id_key_n
and ci.cus_set_c = cim.cus_set_c
and ci.cus_set_c = c.cus_set_c
and ci.cps_key_n = c.cps_key_n
)
where exists (
select 1
from cci ci2, ccim cim2
where ci2.cus_id_key_n = cim2.cus_id_key_n
and ci2.cus_set_c = cim2.cus_set_c
and ci2.cus_set_c = c.cus_set_c
and ci2.cps_key_n = c.cps_key_n
)
答案 4 :(得分:3)
更新回答https://stackoverflow.com/a/4184237/565525:
如果您想要多列,可以像这样实现:
update file1
set
(firstfield, secondfield) = (
select 'stuff' concat 'something from file2',
'some secondfield value'
from file2
where substr(file1.field1, 10, 20) = substr(file2.xxx,1,10) )
where
file1.foo like 'BLAH%'
来源:http://www.dbforums.com/db2/1615011-sql-update-using-join-subquery.html#post6257307
答案 5 :(得分:1)
只更新符合条件的行,并避免更新其他行中的空值:
update table_one set field_1 = 'ACTIVE' where exists
(select 1 from table_two where table_one.customer = table_two.customer);
它适用于DB2 / AIX64 9.7.8
答案 6 :(得分:0)
DB2 LUW 9.7上the UPDATE statement的参考文档提供了以下示例:
UPDATE (SELECT EMPNO, SALARY, COMM,
AVG(SALARY) OVER (PARTITION BY WORKDEPT),
AVG(COMM) OVER (PARTITION BY WORKDEPT)
FROM EMPLOYEE E) AS E(EMPNO, SALARY, COMM, AVGSAL, AVGCOMM)
SET (SALARY, COMM) = (AVGSAL, AVGCOMM)
WHERE EMPNO = '000120'
UPDATE后的括号可以包含一个完整的选择,这意味着任何有效的SELECT语句都可以去那里。
基于此,我建议如下:
UPDATE (
SELECT
f1.firstfield,
f2.anotherfield,
f2.something
FROM file1 f1
WHERE f1.firstfield like 'BLAH%'
INNER JOIN file2 f2
ON substr(f1.firstfield,10,20) = substr(f2.anotherfield,1,10)
)
AS my_files(firstfield, anotherfield, something)
SET
firstfield = ( 'BIT OF TEXT' || something )
编辑:伊恩是对的。我的第一直觉是尝试使用subselects:
UPDATE file1 f1
SET f1.firstfield = ( 'BIT OF TEXT' || (
SELECT f2.something
FROM file2 f2
WHERE substr(f1.firstfield,10,20) = substr(f2.anotherfield,1,10)
))
WHERE f1.firstfield LIKE 'BLAH%'
AND substr(f1.firstfield,10,20) IN (
SELECT substr(f2.anotherfield,1,10)
FROM file2 f2
)
但我不确定连接是否有效。它还假设子串之间存在1:1的映射。如果有多个匹配的行,则无效。
答案 7 :(得分:0)
你问
update file1 f1
set file1.firstfield=
(
select 'BIT OF TEXT' || f2.something
from file2 f2
where substr(f1.firstfield,10,20) = substr(f2.anotherfield,1,10)
)
where exists
(
select * from file2 f2
where substr(f1.firstfield,10,20) = substr(f2.anotherfield,1,10)
)
and f1.firstfield like 'BLAH%'
如果连接给出多个结果,您可以像这样强制更新
update file1 f1
set file1.firstfield=
(
select 'BIT OF TEXT' || f2.something
from file2 f2
where substr(f1.firstfield,10,20) = substr(f2.anotherfield,1,10)
fetch first rows only
)
where exists
(
select * from file2 f2
where substr(f1.firstfield,10,20) = substr(f2.anotherfield,1,10)
)
and f1.firstfield like 'BLAH%'
模板方法
update table1 f1
set (f1.field1, f1.field2, f1.field3, f1.field4)=
(
select f2.field1, f2.field2, f2.field3, 'CONSTVALUE'
from table2 f2
where (f1.key1, f1.key2)=(f2.key1, f2.key2)
)
where exists
(
select * from table2 f2
where (f1.key1, f1.key2)=(f2.key1, f2.key2)
)
答案 8 :(得分:-3)
在标准SQL中,此类更新如下所示:
update a
set a.firstfield ='BIT OF TEXT' + b.something
from file1 a
join file2 b
on substr(a.firstfield,10,20) =
substr(b.anotherfield,1,10)
where a.firstfield like 'BLAH%'
通过较小的语法变体,这种类型的东西可以在Oracle或SQL Server上运行,并且(尽管我没有可用于测试的DB / 2实例)几乎肯定会在DB / 2上运行。