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

  • Home
  • SEARCH
  • 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 8214677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:32:11+00:00 2026-06-07T11:32:11+00:00

SELECT id, FIO, parent_id FROM users WHERE parent_id = ( SELECT id FROM users

  • 0
SELECT id, FIO, parent_id
FROM users
WHERE parent_id =
(
    SELECT id
    FROM users
    WHERE parent_id =
    (
        SELECT id
        FROM users
        WHERE id = 16
    )
)

So here I am making an hierarchy tree, first selecting the root parent, then the children’s and so on to 24th level of depth.

The question is: How to select more than one column from the inner queries?

Because I need to get the other rows fields to display info like: name, surname, age

It looks like I can only get those columns of rows in the outer query (the topmost).

P.S.: I don’t want to use joins because they generate duplicate fields.

Is there a solution?

  • 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-06-07T11:32:14+00:00Added an answer on June 7, 2026 at 11:32 am

    You could iterate on the SQL side using MySQL query variables. This will return all childs with all data of one parent node without repeating yourself (and thus without imposing a limit on the depth of your tree)

    something like this: (500 being the parents id to start with)

    SELECT
      id,
      parent_id, 
      name, 
      '0' as depth, 
      @tree_ids := id AS foo
    FROM 
       tree,
      (SELECT @tree_ids := '', @depth := -1) vars
    WHERE id = 500
    UNION
    SELECT 
      id,
      parent_id,
      name,
      @depth := IF(parent_id = 500, 1, @depth + 1) AS depth,
      @tree_ids := CONCAT(id, ',', @tree_ids) AS foo
    FROM 
      tree 
    WHERE FIND_IN_SET(parent_id, @tree_ids) OR parent_id = 500
    

    See a working example at SQLfiddle

    Note that this gives a really bad performance on larger datasets because MySQL will not use your indexes and instead will do a full table scan. (i don’t understand why its not using indexes, thats just how it is. if someone has advice on or explain the indexing issue, please comment!)

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

Sidebar

Related Questions

SELECT * From `users` AS `User` LEFT JOIN `selections` AS `Selections` ON (`Selections`.`user_id` =
SELECT COUNT(id), AgeRange FROM ( select id, case when age < 0 then 'less
select picks.`fbid`, picks.`time`, categories.`name` as cname, options.`name` as oname, users.`name` from picks left join
select homephone,lastName from DEBTOR where contains((lastName,homephone),'Smith AND 99 999 9999') The first query returns
SELECT users.id, COUNT(?) FROM orders INNER JOIN users ON (orders.user_id = users.id) WHERE ??
SELECT a.*, b.*, b.id AS user_id, a.id AS group_id FROM groups a, users b
public function get_children($id) { $query = $this->db->query(SELECT @last_id := id AS id, parent_id, FIO,
SELECT OrderNumber, FIRST(ShippingName) as ShippingName FROM Orders GROUP BY OrderNumber This gives an error,
SELECT REPLACE([strUrl], '/myoldurl', '/mynewurl') FROM [UrlRewrite] If strUrl is /myoldurl/myoldurl_something_else , it returns /mynewurl/mynewurl_something_else
SELECT Id,Date,Name FROM people WHERE DATEPART(hh,Date) >= 7 AND DATEPART(hh,Date) <= 8 Order by

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.