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?
If you have only need a single level of parent-child relationships (no grandchildren, grandgrand.., etc)
then you can simply do a self join:
If you want to go trough the whole parent-child tree, Oracle and MSSQL have mechanisms that enable you to do that. I’m not familiar with such an option for MySQL.
There more info here:
http://ftp.ntu.edu.tw/MySQL/tech-resources/articles/hierarchical-data.html
http://stackoverflow.com/questions/53108/is-it-possible-to-make-a-recursive-sql-query
Edit:
Ok, some data sanitation first: I do not know if this is an artefact of my import process or if it’s the same at your end, but the parent_sku fields had a newline tacked on. Naturally this caused a failure trying to match a child’s ABC parent_sku to a parents ABC\n SKU. Verify this with
select * from TableA where parent_sku like '%\n'. If any columns match, runSET SQL_SAFE_UPDATES=0;
UPDATE
TableASETparent_sku= REPLACE(REPLACE(parent_sku, ‘\r’, ”), ‘\n’, ”) ;This will remove the newlines.
Same goes for empty string parent_sku’s – if they aren’t null at your end, make them null.
Here is the revised query – I’ve added some pre-join filtering and it’s faster now. Just double check your \n’s and NULLs and it should produce the correct output:
results