Skip to content
Advertisement

SQL – complex SELECT query to UPDATE

SQL noob here. So i have a table with a lot of products. Some of these products are clothing, meaning i have e.g. six different SKU’s for one product due to different sizes. I now want to set the first product variant as the parent for each group. Product IDs and therefore the parent IDs are UUIDs. I managed to write a SELECT query which is taking all the product numbers, finds all groups (by cutting off the last pair of numbers) and assigns all the respective parent (uu)IDs and parent product numbers (for human readable comparison) – it works absolutely fine. But i have no clue on how to convert this rather complex SELECT into an UPDATE. Anyone having ideas? Version is MySQL 8

Table1 looks like this (with all unused columns cut out):

My SELECT query:

I want to update the parent_id so that Table1 looks like this:

It works in the SELECT query, i just don’t know how to make an UPDATE out of this.

Sample table with UUIDs switched for string:

Advertisement

Answer

You just need to slightly adapt your query and set the parentUuid in the update statement, where the product uuid matches.

In the example code below I adapted your query to get a mapping between the products uuid and the parent uuid. Then I update the table setting the parent_id from the product-table where the products uuid matches the product uuid from the query.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement