【问题标题】:Update table with where field from different table [duplicate]使用来自不同表的 where 字段更新表 [重复]
【发布时间】:2019-01-03 18:52:18
【问题描述】:

所以我有 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 的。

【问题讨论】:

    标签: sql oracle join sql-update


    【解决方案1】:

    我不知道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'
    

    我希望这行得通!

    新年快乐!

    【讨论】:

    • 啊,非常感谢您的帮助,但它现在给了我这个错误:ORA-00933:SQL 命令未正确结束。也祝你新年快乐,尼古拉斯 :)
    • @sania247 我在思考和阅读 Oracle 不支持更新时的 JOIN 语句。但是如果我们使用子查询来检测我们需要更新哪些行。例如:update reservation set price = 4000 where r.id in (select r.id from reservation r inner join...)
    猜你喜欢
    • 2018-08-02
    • 2012-10-31
    • 1970-01-01
    • 1970-01-01
    • 2013-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多