我不确定如何使用插入触发器来执行此操作。我想添加一个从“有些复杂”的Select语句插入一些连接的日志,因此我需要表别名。当我运行测试插入时,我得到了。基本上,所有列都需要选择NEW。[表别名]。[列名],但是为了简单起见,仅添加了SELECT NEW.p.passenger_id,NEW.p.first_name
Error Code: 1054. Unknown column 'NEW.p.passenger_id' in 'field list'
这是代码
DROP TRIGGER IF EXISTS trip_after_insert;
DELIMITER //
CREATE TRIGGER trip_after_insert
AFTER INSERT ON passenger_schedule
FOR EACH ROW
BEGIN
INSERT INTO trip_purchase_audit
(passenger_id,
first_name,
last_name,
train_id,
sequence,
car_number,
from_station_id,
start_station_name,
to_station_id,
to_station_name,
ticket_cost,
travel_date,
action_type,
action_date)
SELECT NEW.p.passenger_id, NEW.p.first_name, p.last_name, ps.train_id, ps.sequence, car_number,
ts.station_start_id, s1.station_name as station_start, ts.station_end_id, s2.station_name as station_end,
ps.ticket_cost, ps.travel_date, 'TICKET PURCHASE', NOW()
FROM passenger_schedule ps
JOIN passengers p ON ps.passenger_id = p.passenger_id
JOIN train_schedule ts ON ps.train_id = ts.train_id
AND ps.sequence = ts.sequence
AND ps.ticket_cost = ts.ticket_cost
RIGHT JOIN stations s1 ON ts.station_start_id = s1.station_id
RIGHT JOIN stations s2 ON ts.station_end_id = s2.station_id;
END//
INSERT INTO passenger_schedule
VALUES
(5007, 4, 2, 4, 8, '2019-01-21'),
(5008, 2, 1, 1, 9, '2019-01-21');