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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:08:01+00:00 2026-05-24T21:08:01+00:00

I have a table structure which looks like id | parent | online |

  • 0

I have a table structure which looks like

id | parent | online | name
1  | 1      | 1      | first
2  | 1      | 1      | second
3  | 1      | 1      | third

Basically what I want is, if a column is set to offline then we don’t select that column,
easy enough

Select * from `table` where `online` = 1;

Next part of it becomes, if a columns parent is set to offline, we don’t want to select the column either.

My working is

SELECT t1.* FROM `table` t1 LEFT JOIN `table` t2 ON t1.`parent` = t2.`id` 
WHERE t1.`online` = 1 AND t2.`online` = 1; 

Which works fine if the nesting is at most one level deep.
Is there a better way of doing this if the nesting is only one level deep?

other point is, is there a better way of doing this so that the nesting can be an unlimited amount of levels deep?

If there is the structure would need to allow simple (both load wise and code wise) queries to get all children of a row, and to get all parents of a row with the nesting level relative to the child (e.g. first parent’s level would be 1, parent of parent would be 2).

  • 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-24T21:08:03+00:00Added an answer on May 24, 2026 at 9:08 pm

    I can suggest an answer the first part where the nesting is one-level deep:

    SELECT t1.* 
    FROM `table` t1 
       INNER JOIN `table` t2 ON t1.`parent` = t2.`id`  AND t2.`online` = 1
    WHERE t1.`online` = 1
    

    I believe will be slightly more efficient, for the reason that you’re increasing the cardinality of the statement.

    Unfortunately, multi-level nesting is a tricky subject, I’d do it on SQL Server using a CTE, but I don’t know about this in MySQL, sorry.

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

Sidebar

Related Questions

I have a bag whose structure looks like this: <bag name=foo fetch=select table=table_of_foos> <key
I have a table structure that looks like: <table> <tr id=row1> <td> <div>row 1
I have a table structure with columns like this [ID] [Name] [ParentId] [ParentName] The
I have a StaffLookup table which looks like this. UserSrn | UserName | ManagerSrn
I have a PDO prepared statement which looks like this: $STH = $DBH->(CREATE TABLE
I have a table structure that looks like this: Columns: Account Number Service Code
I have the following table structure, which is imported into an Entity Framework project:
I have the following html structure (which is generated and I can't change): <table
I have a table in my database which stores a tree structure. Here are
I have a database structure that has a Person table which contains fields such

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.