我需要表格:
Table1
--------------------------------
ID VAL1 DATE1
--------------------------------
1 1 20/03/2015
2 null null
3 1 10/01/2015
4 0 12/02/2015
5 null null
Table2
--------------------------------
ID VAL2 DATE1
--------------------------------
1 N 02/06/2015
2 null null
3 O 05/04/2015
4 O 01/07/2015
5 O 03/02/2015
我想更新:
(这两个表不是那么简单,只是为了说明,它们可以与ID列连接)。
所以预期的结果是:
Table1
--------------------------------
ID VAL1 DATE1
--------------------------------
1 1 20/03/2015
2 null null
3 0 05/04/2015
4 0 01/07/2015
5 0 03/02/2015
这是我的代码:
UPDATE Table1 t1
SET t1.VAL1 = '0',
SET t1.DATE1 = (SELECT t2.DATE2 from Table2 t2
INNER JOIN Table1 t1
ON trim(t2.ID)=trim(t1.ID))
WHERE EXISTS (SELECT NULL
FROM Table2 t2
WHERE trim(t2.ID) = trim(t1.ID)
AND t2.Table2 = 'O');
使我的代码无效的部分是:( esle working)
SET t1.DATE1 = (SELECT t2.DATE2 from Table2 t2
INNER JOIN Table1 t1
ON trim(t2.ID)=trim(t1.ID))
答案 0 :(得分:2)
您需要相关子查询,而不是子查询中的join
。因此,假设其余逻辑是正确的:
UPDATE Table1 t1
SET t1.VAL1 = '0',
t1.DATE1 = (SELECT t2.DATE2
FROM Table2 t2
WHERE trim(t2.ID) = trim(t1.ID))
WHERE EXISTS (SELECT 1
FROM Table2 t2
WHERE trim(t2.ID) = trim(t1.ID) AND
t2.Table2 = 'O'
);