Byłoby to znacznie prostsze, gdybyś miał klucz podstawowy:
CREATE TABLE Purchasing (
purchasing_id int auto_increment primary key,
campaign VARCHAR(255),
main_event VARCHAR(255),
sub_event VARCHAR(255),
quantity VARCHAR(255)
);
Z tym prostym dodatkiem:
SELECT p.*
FROM Purchasing p
WHERE p.purchasing_id = (SELECT p2.purchasing_id
FROM Purchasing p2
WHERE p2.campaign = p.campaign
ORDER BY field(p2.main_event, 'Storing', 'Ordered', 'Offered'),
field(p2.sub_event, 'Completed', 'Recorded', 'Delivered')
LIMIT 1
);
Tutaj to db<>skrzypce.