gpt4 book ai didi

mysql - UPDATE FROM WHERE 子查询 SQL Server

转载 作者:行者123 更新时间:2023-11-29 19:15:51 25 4
gpt4 key购买 nike

如何使用 UPDATE 语句中的子查询?这是我的查询:

UPDATE car_availability
SET availability_status = 'GOOD'
FROM car_trip AS TRIP
WHERE car_availability.car_no = TRIP.car_no
AND NOT EXISTS (SELECT DISTINCT A.car_No
FROM car_maintenance A
WHERE A.car_no = TRIP.vehicle_id
AND start_date = TRIP.workday)
AND EXISTS (SELECT DISTINCT B.car_No
FROM car_maintenance B
WHERE B.car_no = TRIP.vehicle_id
AND end_date IS NOT NULL)

我收到错误“UPDATE 和 DELETE 语句中的 FROM 子句不能包含子查询源或联接。”

我想将availability_status更新为GOOD,其中car_maintenance中存在的汽车其开始日期等于car_trip的工作日,并且car_maintenance中的汽车其结束日期不为空。

最佳答案

正如 @McNets 所说,您需要将 car_availability 表添加到 FROM 子句中。试试这个:

UPDATE CA
SET availability_status = 'GOOD'
FROM car_availability CA
JOIN car_trip AS TRIP ON CA.car_no = TRIP.car_no
WHERE NOT EXISTS (SELECT DISTINCT A.car_No
FROM car_maintenance A
WHERE A.car_no = TRIP.vehicle_id
AND start_date = TRIP.workday)
AND EXISTS (SELECT DISTINCT B.car_No
FROM car_maintenance B
WHERE B.car_no = TRIP.vehicle_id
AND end_date IS NOT NULL)

关于mysql - UPDATE FROM WHERE 子查询 SQL Server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42722143/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com