2

假设我有两张桌子:

USER
+------+------+-------------------+
|   ID | NAME | CREATED_TIMESTAMP |
+------+------+-------------------+
|    1 |   N1 |              Null |
+------+------+-------------------+
|    2 |   N2 |              Null | <--------- acceptor of reference-value
+------+------+-------------------+           \
|  ... |  ... |               ... |            \
+------+------+-------------------+             \
                                                 \ 
REFERENCE_CREATED_TIMESTAMPS                      \
+-------------+----+-------------------+            these records match
|  TABLE_NAME | ID |CREATED_TIMESTAMP  |          /
+-------------+----+-------------------+         /
|        USER |  1 | 12.03.12 17:20:40 |        /
+-------------+----+-------------------+       /
|        USER |  2 | 14.03.12 10:12:54 |  <--- reference-value
+-------------+----+-------------------+
|   FOO_TABLE |  1 | 17.05.12 08:10:01 |
+-------------+----+-------------------+
|         ... | ...|               ... |
+-------------+----+-------------------+

现在我想填充 USER 表中的 CREATE_TIMESTAMP 列,从REFERENCE_CREATED_TIMESTAMPS表中获取值,即对于 ID=2 的用户,我将使用CREATED_TIMESTAMPfrom REFERENCE_CREATED_TIMESTAMPSrow whereTABLE_NAME=USER和的值ID=2

对 USER 表的所有记录执行此操作的查询是什么?

4

3 回答 3

4

试试这样。。

Update UserTable Set CREATED_TIMESTAMP=
(Select Min(CREATED_TIMESTAMP) from REFERENCE_CREATED_TIMESTAMPS where
 REFERENCE_CREATED_TIMESTAMPS.Id=UserTable.Id and Table_Name='User')
于 2013-06-17T16:37:26.630 回答
1
update  "User"
set     CREATED_TIMESTAMP =
        (
        select  rct.CREATED_TIMESTAMP
        from    REFERENCE_CREATED_TIMESTAMPS rct
        where   rct.ID = "User".ID
                and rct.TABLE_NAME = 'USER'
        );

SQL Fiddle 的示例。

于 2013-06-17T16:46:02.617 回答
0

像这样的东西:

  update USER u, REFERENCE_CREATED_TIMESTAMPS r
  set u.CREATED_TIMESTAMP = b.CREATED_TIMESTAMP
  where u.ID = r.ID AND r.TABLE_NAME = 'USER'
        AND u.ID = 2;
于 2013-06-17T16:37:40.907 回答