[Solved] How to SELECT and INSERT until ordered quantity is completed using nested query in MySQL

I hope and imagine that there must be a more performant solution, but anyway, consider the following: Schema (MySQL v8.0) DROP TABLE IF EXISTS product_batches; CREATE TABLE product_batches (batch_number SERIAL PRIMARY KEY ,product_id INT NOT NULL ,quantity_available INT NOT NULL ); INSERT INTO product_batches VALUES ( 1,1,15), ( 3,1, 5), ( 7,1,20), (10,1,30), (11,1,50); Query … Read more