0

So, let's say I have a database of fruits, along with recipes based on those fruits.

id fruit name
1 apple
2 pear
id recipe fruit_id
1 apple pie 1
2 baked apple 1
3 poached pear 2

Is it possible, using a single SQL insert in MySQL, to add a new fruit (banana) and 1 or more associated recipes ('bananas foster', 'banana bread')?

Thanks

4
  • Does this answer your question? Insert using LEFT JOIN and INNER JOIN
    – Cid
    Commented Apr 5, 2021 at 19:41
  • 3
    No, it is not possible in a single statement. A MySQL insert only inserts into one table at a time. Commented Apr 5, 2021 at 19:48
  • You need to use two statements. First insert the new fruit. Then insert all the related recipes, using LAST_INSERT_ID() to get the ID that was assigned to the fruit.
    – Barmar
    Commented Apr 5, 2021 at 20:00
  • Thanks - I had thought that two statements were necessary, but hoped there might've been some advanced query that could do it in one Commented Apr 5, 2021 at 20:14

0

Browse other questions tagged or ask your own question.