Trouble with logic for MySQL Query to INSERT into content from one table to another
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 开发者_如何学Pythonbeen 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:
SELECT T.entry_id as child_id, T2.entry_id as parent_id
FROM TableA AS T LEFT JOIN TableA AS T2
ON T.parent_SKU=T2.SKU
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, run
SET SQL_SAFE_UPDATES=0;
UPDATE TableA
SET parent_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:
SELECT child.entry_id as child_id, parent.entry_id as parent_id
FROM
(
select * from TableA
-- only children have parent_sku
where parent_SKU is not null
-- some extra safety for empty strings,
-- can be removed after empties are replaced by NULLs
and length(parent_SKU) > 0 and length(sku)>0 and length(parent_sku)>0
)
AS child
LEFT JOIN
(
select * from tableA
-- only parents don't have parent_sku
where (parent_SKU is null or length(parent_SKU)=0)
and length(sku)>0
) AS parent
ON child.parent_SKU = parent.SKU
results
Maybe I am missing something here, but
select child.entry_id, parent.entry_id
from Table_A child
join Table_A parent
on (child.parent_sku = parent.sku)
where child.isVariant = 'Yes'
精彩评论