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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:00:07+00:00 2026-05-18T22:00:07+00:00

I am working with mysql and php and have a Parent Child table created

  • 0

I am working with mysql and php and have a Parent Child table created in mysql.
Every parent has 3 Childs. Every Child becomes also a Parent and has also 3 Childs and so on.

Now I need to write a function that gives me the last parent in the tree (depending on the selected parent ID) that does not have 3 Childs. So the next Child subscription can be dedicated to that parent.

Can this be done with mysql query or function?

Thanks,
M


I use the following query:

SELECT t1.uid, t2.pid FROM cms_users AS t1 LEFT JOIN cms_users AS t2 ON t1.uid = t2.pid 

Which gives mr the following output:

t1.uid t2.pid
1      1 
1      1 
1      1 
2      2 
2      2 
2      2 
3      3 
4      NULL 
5      NULL

What I actualy need is a result like:

p1.uid p2.pid 
1      3 
2      3 
3      1 
4      0 
5      0 

This result also starts from the root Parent 1
I need to get the results starting from a selected uid somewhere in the three.
Every parent has his own tree starting from his uid.
Probably I need to write a stored procedure or something but this is all new to me and I don’t know how to do this.

This is an example of how the tree looks like. http://www.musafreaks.com/images/tree.jpg
User ID 1 has his own tree, even user 2, 3 and 4 have there own tree and so on.

  • 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-18T22:00:08+00:00Added an answer on May 18, 2026 at 10:00 pm

    This problem is not trivial to solve within MySQL. Multiple strategies to manage hierarchical data are described in the manual. The rest of my answer is based on this article.

    Your setup is sort of a “Adjacency List Model”, so you could adapt the LEFT JOIN solution for your dataset.

    SELECT t1.name FROM
    category AS t1 LEFT JOIN category as t2
    ON t1.category_id = t2.parent
    WHERE t2.category_id IS NULL;
    

    This assumes that the parent field of the root node is null. Your possible new parent will be selected as t1. Note that this query will return all nodes which have no children, if you want to “fill” up each node with three children you’ll have to extend the query a bit.

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

Sidebar

Related Questions

I am using MySQL and PHP for a project I am working. I have
Can a PHP script (which can be working with a MySQL DB) send and/or
I am currently working on a web application that uses PHP and MySQL, but
I'm working with a MySQL database that has some data imported from Excel .
I am working on a script to send data to a mysql table and
I've developed an application at working using MySQL 5, that uses Views to access
I'm working with a MySQL query that writes into an outfile. I run this
I'm a MySQL guy working on a SQL Server project, trying to get a
While working between a Windows MySQL server and a Debian MySQL server, I noticed
I'm working on performance tuning my SQL Server 2008 database and am using the

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.