使用多个条件和子查询进行UPDATE查询

时间:2017-04-14 17:10:46

标签: sql ms-access

我正在尝试从具有多个条件的表中更改数据,如下所示:

UPDATE field0 as f0
 SET f0.c=IIF(Table.Field="x","a",IFF(Table.Field="y","b","c"))
FROM (
  SELECT f2.b AS Field
  FROM (field1 as f1 INNER JOIN field2 as f2 ON f1.a=f2.a)
  INNER JOIN field0 as f00 f00.a=f1.a
) as Table
;

但它没有用,它说我的查询方式有问题。

我也尝试过这样:

UPDATE field0 as f0
SET f0.c=IIF(f0.a=ANY(
 SELECT f1.c
 FROM field1 as f1 INNER JOIN field2 as f2 ON f1.a=f2.a
 WHERE f2.b=4),"a",IIF(f0.a=ANY(
  SELECT f1.c
  FROM field1 as f1 INNER JOIN field2 as f2 ON f1.a=f2.a
  WHERE f2.b NOT NULL),"b","c"))
;

但是这个说查询不是可更新的查询:/

我不知道该怎么做,有什么帮助吗?

这是我发现的实际代码: UPDATE Flights AS F SET F.status = (SELECT IIF(DE.handeling_code=4,"canceled",IIF(DE.handeling_code IS NOT NULL,"on time ","late")) FROM Irregular_Events AS IE INNER JOIN Delay_Event AS DE ON IE.IE_code=DE.delay_code WHERE IE.flight_code=F.flight_code ORDER BY F.flight_code); 我试图这样做,但它说当我想使用查询:“操作必须使用可更新的查询”

这也是我的航班表数据(以简化的方式): flight_code|flight_status -----------|------------- 1 | On time 2 | On time 3 | On time 4 | On time irregular_event表是这样的: IE_code|flight_code -------|----------- 1 | 2 2 | 4 delay_event表是这样的: delay_code|delay_reason ----------|------------ 1 | 1 2 | 4 延迟原因从1到3只是延迟,4是取消航班 所以我需要将航班2状态更改为延迟,将航班4更改为取消。

很抱歉这个杂乱的问题是我第一次来这里,我真的需要帮助解决这个问题,请跟我一起轻松!

2 个答案:

答案 0 :(得分:0)

第二个Iif出错,你有IFF并且缺少嵌套Iif的关闭窗口。

也许你应该使用WHERE IN:

UPDATE field0 as f0
 SET f0.c=Iif(Table.Field="x","a",Iif(Table.Field="y","b","c"))
WHERE b IN (
  SELECT f2.b AS Field
  FROM (field1 as f1 INNER JOIN field2 as f2 ON f1.a=f2.a)
  INNER JOIN field0 as f00 f00.a=f1.a)

答案 1 :(得分:0)

别介意我以某种方式得到了解决方案并做到了这一点: UPDATE Flights AS F, Irregular_Events AS IE, Delay_Event AS DE SET F.status = IIF(DE.handeling_code=4,"Canseled","Delayed") WHERE F.flight_code=IE.flight_code AND IE.IE_code=DE.delay_code; 它工作正常!