To by bylo mnohem jednodušší, kdybyste měli primární klíč:
CREATE TABLE Purchasing (
purchasing_id int auto_increment primary key,
campaign VARCHAR(255),
main_event VARCHAR(255),
sub_event VARCHAR(255),
quantity VARCHAR(255)
);
S tímto jednoduchým dodatkem:
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
);
Zde je db<>housle.