I have 2 tables. The first table is tbl_items
and the second table is tbl_items_extras
.
I insert my Items into tbl_items
and if in case this item has a single/multiple extras, they will be inserted in the table tbl_items_extras
where tbl_items.id=tbl_items_extras.tbl_items_id
.
Below is the screenshot of my two tables.
I was able to duplicate the records for tbl_items
perfectly using the below query;
INSERT INTO `tbl_items` (`items_ref_id`, `rev`, `die_number`, `product_type_id`, `parts_id`, `complexity_id`)
SELECT
q.`items_ref_id`,
q.`rev`+1,
q.`die_number`,
q.`product_type_id`,
q.`parts_id`,
q.`complexity_id`
FROM `tbl_items` q WHERE `items_ref_id`='$refNum' AND `rev`='$maxRev'
But when I duplicate the tbl_items_extras
records using the below query;
INSERT INTO `tbl_items_extras` (`tbl_items_id`, `extras_conditions_id`, `percentage`, `quantity`, `total_percentage`)
SELECT
q.`tbl_items_id`,
q.`extras_conditions_id`,
q.`percentage`,
q.`quantity`,
q.`total_percentage`
FROM `tbl_items_extras` q WHERE `tbl_items_id` IN (SELECT `id` FROM `tbl_items` WHERE `items_ref_id`='$refNum' AND `rev`='$rev')
In this query, I didn't get what I need exactly. See the below screenshot.
What I need is to duplicate selected records in tbl_items_extras
where the tbl_items_id
has to be the SQL_INSERTED_ID
of the duplicated records in tbl_items
.
The result should be as per the below screenshot.
As you can see, the tbl_items.id
was used accordingly in the tbl_items_extras.tbl_items_id
.
I know I can use the LAST_INSERT_ID
but this works only for a single record.