首页 > 解决方案 > 使用来自不同表的 where 字段更新表

问题描述

所以我有 3 张桌子预订,航班和机场,我需要根据它所在的城市(城市字段在机场)从预订中更新价格。

这是我创建表以显示表之间的链接:

create table airport
(airport_code varchar(100) primary key,
city varchar(100),
country varchar(100) );

create table flight
(flight_code varchar(100) primary key, 
dept_airport_code varchar(100),
arr_airport_code varchar(100),
foreign key (dept_airport_code)  references  airport (airport_code),
foreign key (arr_airport_code)  references  airport (airport_code) );

create table reservation
(flight_code varchar(100),
reservation add price integer);

这是我到目前为止所尝试的,基于以前对类似问题的回答,但它不起作用。

update reservation r inner join flight f on r.flight_code=f.flight_code
inner join airport a on f.dept_airport_code=a.airport_code
set r.price=4000 where a.city='Dubai';

它给了我这个错误:ORA-00971:缺少 SET 关键字

我认为这是因为 Oracle 不接受这种语法,而这些答案是针对 MySQL 的。

标签: sqloraclejoinsql-update

解决方案


我不了解 Oracle 语法,但您可以尝试 T-SQL 方式:

update r 
set    r.price=4000
from   reservation 
       inner join flight f on r.flight_code=f.flight_code
       inner join airport a on f.dept_airport_code=a.airport_code
where  a.city='Dubai'

我希望这行得通!

新年快乐!


推荐阅读