I am creating a stored procedure which will populate my table with data passed from procedure argument and from other table.
I have a table with three columns (cart_id, product, sold_quantity). Now, I want to populate this table with a stored procedure. In this table, cart_id
will be passed as stored procedure argument while other two columns are populated from another table. I want to insert latest 10 rows from other table and for all these rows, the cart_id
will be same
First I tried,
BEGIN
insert into my_table (cart_id, product, sold_quantity) VALUES (cart_id, (Select `product`, `sold_quantity` from other_table limit 10))
END
Here, cart_id is passed in procedure argument. This obviously does not work as the coulmn count does not match. So, I tried,
BEGIN
insert into my_table (cart_id, product, sold_quantity) VALUES (cart_id, (Select `product` from other_table limit 10), (Select `sold_quantity` from other_table limit 10))
END
This returns error Subquery returns more than 1 row
Is there any way I can populate my table with data from a combination of argument parameter and other table?
INSERT ... SELECT ...
which is what that answer shows.