如果在具有相同日期的其他表中找到,则更新一个表中的日期字段

时间:2014-01-30 20:57:40

标签: sql oracle

。我正在使用此查询,但它无法正常工作。关于这里有什么问题的任何想法或建议;

'UPDATE SAMPLE C SET SAMPLE.C_DATE = TO_DATE('01/01/2013','MM/DD/YYYY') and TO_DATE('12/31/2013','MM/DD/YYYY') WHERE EXISTS (SELECT * FROM MEMBER_A A, S_NEW B WHERE A.MEM_NBR = B.MEM_NBR AND B.M_ID IN (1111,2222,3333) AND A.L_TEST = TO_DATE('01/01/2013','MM/DD/YYYY') and TO_DATE('12/31/2013','MM/DD/YYYY') AND B.ID = C.ID)

我无法更新它,我收到了错误消息。

有关如何改善这一点的任何想法。 谢谢,

2 个答案:

答案 0 :(得分:0)

你有一个根本问题。您要将该值设置为什么日期?我猜第一个。

还有一些其他语法问题。以下可能是您想要的:

UPDATE SAMPLE
    SET C_DATE = TO_DATE('01/01/2013', 'MM/DD/YYYY')
    WHERE EXISTS (SELECT *
                  FROM MEMBER_A A JOIN
                       S_NEW B
                       ON A.MEM_NBR = B.MEM_NBR AND
                  WHERE B.M_ID IN (1111,2222,3333) AND
                        A.L_TEST BETWEEN TO_DATE('01/01/2013', 'MM/DD/YYYY') and TO_DATE('12/31/2013', 'MM/DD/YYYY') AND
                        B.ID = SAMPLE.ID
                 );

编辑:

在Oracle中,您可以执行超级优雅的双子查询解决方案:

UPDATE SAMPLE
    SET C_DATE = (SELECT max(A.L_TEST)
                  FROM MEMBER_A A JOIN
                       S_NEW B
                       ON A.MEM_NBR = B.MEM_NBR AND
                  WHERE B.M_ID IN (1111,2222,3333) AND
                        A.L_TEST BETWEEN TO_DATE('01/01/2013', 'MM/DD/YYYY') and TO_DATE('12/31/2013', 'MM/DD/YYYY') AND
                        B.ID = SAMPLE.ID
                 )
    WHERE EXISTS (SELECT *
                  FROM MEMBER_A A JOIN
                       S_NEW B
                       ON A.MEM_NBR = B.MEM_NBR AND
                  WHERE B.M_ID IN (1111,2222,3333) AND
                        A.L_TEST BETWEEN TO_DATE('01/01/2013', 'MM/DD/YYYY') and TO_DATE('12/31/2013', 'MM/DD/YYYY') AND
                        B.ID = SAMPLE.ID
                 );

max()只是为了确保只返回一个值。

答案 1 :(得分:0)

您正在将c_date与两个日期进行比较。这不起作用。 **之间的代码需要你注意。从你的问题不清楚如何处理它。

UPDATE SAMPLE C
SET SAMPLE.C_DATE = **TO_DATE('01/01/2013','MM/DD/YYYY') and TO_DATE('12/31/2013','MM/DD/YYYY')**
WHERE EXISTS
( SELECT *
  FROM   MEMBER_A A
  ,      S_NEW B
  WHERE  A.MEM_NBR = B.MEM_NBR
  AND    B.M_ID IN (1111,2222,3333)
  AND    A.L_TEST = **TO_DATE('01/01/2013','MM/DD/YYYY') and TO_DATE('12/31/2013','MM/DD/YYYY')**
  AND B.ID = C.ID
)