Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6567033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:17:18+00:00 2026-05-25T14:17:18+00:00

I wonder if anyone could assist? Table_A below shows an example product in my

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:17:19+00:00Added an answer on May 25, 2026 at 2:17 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wonder if anyone could help me with a problem I've been having. I
Hi wonder if anyone could help me. the first code below basically counted every
Hi I wonder if anyone could give me an example how the TickGetDiv256(); function
I wonder if anyone can help me to understand where I could be going
I wonder if anyone has faced the same problem. When we done some modification
I wonder if anyone could point me in the direction where I can read
I wonder if anyone could help me. I have a web page where there
I wonder if anyone knows how could I rename multiple files, all of them
I have a question regarding AS3 memory management I wonder if anyone could help
I've got a little history in CMS technology and I wonder if anyone could

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.