I wonder if anyone could assist? Table_A below shows an example product in my MySQL product database. I'm having problem working out the query logic to achieve Table_B below. (It's not the INSERT command I'm having a problem with.)
Table_A
entry_id | SKU | Product | Parent_SKU | IsVariant
----------------------------------------------------------------
1 | ABC | Green T-Shirt | | No
2 | ABCS | Green T-Shirt S | ABC | Yes
3 | ABCM | Green T-Shirt M | ABC | Yes
4 | ABCL | Green T-Shirt L | ABC | Yes
Table_B
child entry_id | parent entry_id
----------------------------------
2 | 1
3 | 1
4 | 1
So far, here's the query I've been running.
INSERT INTO Table_B
(parent_entry_id, child_entry_id)
SELECT
Table_A.entry_id,
(SELECT Table_A.entry_id FROM Table_A
WHERE Table_A.SKU = Table_A.Parent_SKU
AND Table_A.IsVariant = 'No')
FROM Table_A
WHERE IsVariant = 'Yes'
I know full well the logic is wrong, but after a day of trying to think it through my brain is frazzled. I've searched on here and other places as best as I can think, but without any thoughts or answers to help. So can anyone here assist? Should I be thinking the query the another way round?