Oracle UPDATE使用包含JOIN的SELECT语句

时间:2015-08-25 13:58:49

标签: sql oracle join sql-update

我正在向Oracle移植H2 UPDATE / SELECT语句,并且遇到了我在诊断时遇到问题的语法问题。

我有3个表,UsersCarsTrips。他们看起来像:

SELECT * FROM USERS;
+------------+----+--------+
|  USERNAME  | ID | CAR_ID |
+------------+----+--------+
| John.Smith |  1 |     5  |
| Abby.Smith |  2 |     6  |
+------------+----+--------+

SELECT * FROM CARS;
+----+--------+-------+
| ID |  MAKE  | COLOR |
+----+--------+-------+
|  5 | Subaru | Green |
|  6 | Honda  | Red   |
+----+--------+-------+

SELECT * FROM TRIPS;
+----+------------+---------+
| ID |  USERNAME  | MILEAGE |
+----+------------+---------+
|  8 | Abby.Smith |      87 |
|  9 | John.Smith |      23 |
+----+------------+---------+

但是,我们现在决定向Trips添加一个新列,并且(仅)跟踪汽车模型而不是用户。作为中间结果,在放弃USERNAME列之前,我正试图实现:

+----+------------+---------+-----------+
| ID |  USERNAME  | MILEAGE | CAR_MODEL |
+----+------------+---------+-----------+
|  8 | Abby.Smith |      87 |     Honda |
|  9 | John.Smith |      23 |    Subaru |
+----+------------+---------+-----------+

我的(非功能性)陈述如下:

WITH USER_MODELS AS
(SELECT USERNAME, MAKE FROM USERS JOIN CARS ON (USERS.CAR_ID = CARS.ID))
UPDATE TRIPS SET CAR_MODEL =
    SELECT MAKE FROM USER_MODELS
    WHERE TRIPS.USERNAME = USER_MODELS.USERNAME

但是,我收到的语法错误是:

UPDATE TRIPS SET CAR_MODEL =
*
ERROR at line 3:
ORA-00928: missing SELECT keyword

这让我失去了一个循环。 SELECT语句紧跟在UPDATE之后,WITH本身前面有SELECT子句,似乎包含格式正确的$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

1 个答案:

答案 0 :(得分:2)

可以这样做,但是你需要将WITH子句放在子查询中:

timer = [NSTimer scheduledTimerWithTimeInterval:60.0f target:self selector:@selector(timeUpdated) userInfo:nil repeats:YES];