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 7593941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:17:48+00:00 2026-05-30T21:17:48+00:00

I currently have a table which has these columns: id (INT) parent_id (INT) col0

  • 0

I currently have a table which has these columns:

id (INT)
parent_id (INT)
col0
col1
col2

As an example there are the following entries saved in this table:

1     NULL   abc     def     NULL
2     1      test    NULL    NULL
3     1      NULL    NULL    xyz

Now I’d like to search in all rows A which haven’t got any rows B which are pointing to them (B.parent_id = A.id). In addition the row values should be either the ones that are present in the current row or if there is a NULL, the values of the parent should be considered.

To illustrate my requirements I’d like to show some examples:

SEARCH(col0=test) => #2 (#1 has some children, #3.col0 = abc (inherited from #1))
SEARCH(col1=def) => #2, #3 (#1 has some children)
SEARCH(col2=xyz) => #3 (#1 has some children, #2.col2 = NULL (inherited from #1))

Does anyone know how to implement such a search in MySQL?

  • 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-30T21:17:49+00:00Added an answer on May 30, 2026 at 9:17 pm
    SELECT
    # if first table has no value, use parent table
    IF(t1.col0, t1.col0, t2.col0) as virtcol0,
    IF(t1.col1, t1.col1, t2.col1) as virtcol1,
    IF(t1.col2, t1.col2, t2.col2) as virtcol2
    FROM table as t1
    LEFT JOIN table as t2 ON t1.parent_id = t2.id
    LEFT JOIN table as t3 ON t1.id = t3.parent_id
    # t3 would be children of t1. We don't want t1 to procreate. :)
    WHERE t3.id IS NULL
    # Your actual search goes here:
    AND virtcol0/1/2 = whatever
    

    Fast? No. Best index use you can get out of this are the joins on id/parent_id.

    If you have a lot of data and small result sets, you can query the columns directly on an index and then run checks for parents and children in separate queries. That’d be a lot faster than running the above query on a huge table.

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

Sidebar

Related Questions

I currently have a table which only has a single editable column. I have
I have a table in MySQL 4.0 which currently has a year field as
I'm currently dynamically building an asp:table which contains checkboxes these checkboxes have a CheckChanged
I currently have a table that is 17 columns wide and has 30 records.
I have a table which has two fields. There can be multiple rows with
I currently have a site with a table that has Lat/Long float columns, and
I currently have a user's table which contains a one-to-one relationship for Youtube OAuth
I have an application in which I'm currently using a two column table in
I have a table with 4 columns, id , Stream which is text, Duration
I'm currently working with MS SQL 2005, and have a table that has 17

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.