我正在建立一个大学停车申请数据库。 我已经列出了3个必要的表格:
SPACE(Id(pk),PassRequired,OccupiedBy(fk-->VEHICLE.Id))
PARKING_VIOLATION(Id(pk),DateIssued,Description,Cost,VehicleReceived(fk-->VEHICLE.Id))
VEHICLE (Id(pk),PassId(fk-->PARKING_PASS.Id))
如果SPACE中的车辆无法有效停放,我需要一个插入我们的PARKING_VIOLATION表的查询。例如,如果他们的PARKING_PASS.PassId与SPACE.PassRequired不匹配。我想运行一个每5分钟运行一次这个查询的SQL作业,但是在向某人发票后它没有检测到重复。如果车辆已在同一天出票,则重复。
INSERT INTO PARKING_VIOLATION(Id, DateIssued, Description, Cost, VehicleReceived)
(SELECT NULL,CURRENT_TIMESTAMP,'Invalid Parking Pass','75', VEHICLE.Id
FROM SPACE
INNER JOIN VEHICLE ON (SPACE.OccupiedBy=VEHICLE.Id)
INNER JOIN PARKING_VIOLATION ON (VEHICLE.Id=VehicleReceived)
WHERE (VEHICLE.PassId != SPACE.PassRequired)
AND VehicleReceived NOT IN(
SELECT VehicleReceived FROM PARKING_VIOLATION
WHERE (CAST(PARKING_VIOLATION.DateIssued AS DATE) = CURDATE())
));
我的查询现在返回所有停车无效的车辆(VEHICLE.PassId!= SPACE.PassRequired)并且不在今天的PARKING_VIOLATIONS列表中。但是,它在运行时没有插入任何数据,而且我有适合条件的数据。
我也尝试此查询并接收"您无法在FROM子句中指定目标表PARKING_VIOLATION以进行更新
INSERT INTO PARKING_VIOLATION(Id, DateIssued, Description, Cost, VehicleReceived)
VALUES (NULL,CURRENT_TIMESTAMP,'Invalid Parking Pass','75', (SELECT
VEHICLE.Id
FROM SPACE
INNER JOIN VEHICLE ON (SPACE.OccupiedBy=VEHICLE.Id)
INNER JOIN PARKING_VIOLATION ON (VEHICLE.Id=VehicleReceived)
WHERE (VEHICLE.PassId != SPACE.PassRequired)
AND VehicleReceived NOT IN(
SELECT VehicleReceived FROM PARKING_VIOLATION
WHERE (CAST(PARKING_VIOLATION.DateIssued AS DATE) = CURDATE())
)));