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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:59:20+00:00 2026-05-14T02:59:20+00:00

Lets say I have the following MySQL structure: CREATE TABLE `domains` ( `id` INT(10)

  • 0

Lets say I have the following MySQL structure:

CREATE TABLE `domains` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`domain` CHAR(50) NOT NULL,
`parent` INT(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MYISAM AUTO_INCREMENT=10 DEFAULT CHARSET=latin1

insert  into `domains`(`id`,`domain`,`parent`) values (1,'.com',0);
insert  into `domains`(`id`,`domain`,`parent`) values (2,'example.com',1);
insert  into `domains`(`id`,`domain`,`parent`) values (3,'sub1.example.com',2);
insert  into `domains`(`id`,`domain`,`parent`) values (4,'sub2.example.com',2);
insert  into `domains`(`id`,`domain`,`parent`) values (5,'s1.sub1.example.com',3);
insert  into `domains`(`id`,`domain`,`parent`) values (6,'s2.sub1.example.com',3);
insert  into `domains`(`id`,`domain`,`parent`) values (7,'sx1.s1.sub1.example.com',5);
insert  into `domains`(`id`,`domain`,`parent`) values (8,'sx2.s2.sub1.example.com',6);
insert  into `domains`(`id`,`domain`,`parent`) values (9,'x.sub2.example.com',4);

In my mind that is enough to emulate a simple tree structure:

            .com
             |             
           example                 
        /          \
      sub1          sub2

ect

My problem is that give sub1.example.com I want to know all the children of sub1.example.com without using multiple queries in my code.

I have tried joining the table to itself and tried to use subqueries, I can’t think of anything that will reveal all the children.

At work we are using MPTT to keep in hierarchal order a list of domains/subdomains however, I feel that there is an easier way to do it.

I did some digging and someone did something similar but they required the use of a function in MySQL. I don’t think for something simple like this we would need a whole function.

Maybe I am just dumb and not seeing some sort of obvious solution.

Also, feel free to alter the structure.

  • 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-14T02:59:20+00:00Added an answer on May 14, 2026 at 2:59 am

    The solution was simple, albeit arguable on its efficiency.

    I have modified the table structure as follows:

    CREATE TABLE `domains` (
      `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
      `domain` CHAR(50) NOT NULL,
      `level` INT(11) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=MYISAM AUTO_INCREMENT=11 DEFAULT CHARSET=latin1
    

    Level pertains to the depth in the tree.

    Sample data:

    insert  into `domains`(`id`,`domain`,`level`) values (1,'.com',0);
    insert  into `domains`(`id`,`domain`,`level`) values (2,'example.com',1);
    insert  into `domains`(`id`,`domain`,`level`) values (3,'sub1.example.com',2);
    insert  into `domains`(`id`,`domain`,`level`) values (4,'sub2.example.com',2);
    insert  into `domains`(`id`,`domain`,`level`) values (5,'s1.sub1.example.com',3);
    insert  into `domains`(`id`,`domain`,`level`) values (6,'s2.sub1.example.com',3);
    insert  into `domains`(`id`,`domain`,`level`) values (7,'sx1.s1.sub1.example.com',4);
    insert  into `domains`(`id`,`domain`,`level`) values (8,'sx2.s2.sub1.example.com',4);
    insert  into `domains`(`id`,`domain`,`level`) values (9,'x.sub2.example.com',3);
    insert  into `domains`(`id`,`domain`,`level`) values (10,'t.sx1.s1.sub1.example.com',5);
    

    So lets say we are given sub1.domain.com and we want to know all of its children the query is rather simple:

    SELECT * FROM domains WHERE domain LIKE "%.sub1.example.com" ORDER BY level;
    

    Of course if we want sub1.example.com in our result set we can just do:

    SELECT * FROM domains WHERE domain LIKE "%sub1.example.com" ORDER BY level;
    

    From the result set we get a list of all the children given a child.

    To delete a child (and all the associated children) is simple and a very similar query

    DELETE FROM domains WHERE domain LIKE "%sub1.example.com";
    

    Insertions are easy, and it just takes 2 queries (assuming the user has a drop down box and chooses the parent):

    SELECT level FROM domains WHERE domain = "sub2.example.com";
    
    INSERT INTO domains (domain, level) VALUES ($sub + ".sub2.example.com", $level+1)
    

    Please excuse the mixed PHP + MySQL syntax, but you get the idea.

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

Sidebar

Related Questions

Lets say I have the following: public class MyObject { [Bindable] public var foo:int
Lets say I have the following mapping: <hibernate-mapping package=mypackage> <class name=User table=user> <id name=id
Lets say I have the following: <table><tr><td> <table> <!-- I want a reference to
Lets say I have a table with some data like the following: ID text
Let's say I have the following MySQL table: id | species ------------ 1 |
Let's say I have the following MySQL table: id tagid campaignid 1 10 1000
Lets say I have a table with the following rows/values: +--------+----------+ | ID |
i want to create a php with mysql to do the following: lets say
Lets say, i have the following table | start | end | activity +---------------------+---------------------+---------
Lets say I have the following code: abstract class Animal case class Dog(name:String) extends

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.