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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:36:39+00:00 2026-06-12T12:36:39+00:00

I need a SQL query for the following COMPLEX task… I need to to

  • 0

I need a SQL query for the following COMPLEX task…

I need to to select from a column named parent_id. If a row has 0 for parent_id it means that it’s a category (it also has the type column which says cat for category), if a row has 1 or more for parent_id it means that it is a rule. The parent_id of a rule, is the rule_id of a category.

The table structure with some data in it:
phpMyAdmin table data

I need to select in a way, that EACH category, has its children under it. From the picture we would get this:

Sample Category 1
   Sample rule 1
Sample Category 2

I tried some some queries that I found from here, but none worked.

This is what I tried last:

'SELECT *
FROM ' . RULES_TABLE . ' AS parent
    LEFT JOIN ' . RULES_TABLE . ' AS child 
    ON child.parent_id = parent.rule_id 
WHERE parent.parent_id = 0
     AND parent.public = 1
ORDER BY parent.cat_position, child.rule_position';

It returned Sample rule 1 only.

Ideas?

  • 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-12T12:36:40+00:00Added an answer on June 12, 2026 at 12:36 pm

    Try this fiddle http://sqlfiddle.com/#!2/0a9c5/16, sql code below

    SELECT c.rule_id, c.rule_title, GROUP_CONCAT(r.rule_title)
    FROM RULES_TABLE AS c
    LEFT JOIN RULES_TABLE AS r
        ON r.parent_id = c.rule_id
    WHERE c.parent_id IS NULL AND c.public = 1
    GROUP BY c.rule_id
    ORDER BY c.cat_position, c.rule_position;
    

    left out php-ization of code on purpose to maintain syntax highlighting, which seems not to work anyway

    If you exceed maximum allowed group_concat size you can either increase it or use the following version, and do a bit more processing in php:

    SELECT c.rule_id, c.rule_title, r.rule_title as r_rule_title
    FROM RULES_TABLE AS c
    LEFT JOIN RULES_TABLE AS r
        ON r.parent_id = c.rule_id
    WHERE c.parent_id IS NULL AND c.public = 1
    ORDER BY c.cat_position, c.rule_position;
    

    and in php, skeleton code provided only, fill in with the actual values, assuming you use pdo and your pdostatement var is named $query:

    $old_rule_id = null;
    $rrt = array(); // will contain all r.rule_titles for a give c.rule_id
    while( ($i = $query->fetch(PDO::FETCH_OBJ)) !== false ) {
        if( $old_rule_id != $i->rule_id ) {
            if( count($rrt) ) {
                echo ... all the data for previous rule from $rrt ...
                echo ... end of previous element ...
                $rrt = array(); // empty it to collect new data
            }
            echo ... start of current element use data from $i->rule_id and $i->rule_title ...
            $old_rule_id = $rule_id;
        }
        $rrt[] = $i->r_rule_title;
    }
    // the following is the same if from inside the while, minus reinitialization of $rrt;
    if( count($rrt) ) {
        echo ... all the data for previous rule from $rrt ...
        echo ... end of previous element ...
    }
    

    replace stuff between … with valid code

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

Sidebar

Related Questions

I need to execute a SQL Query between two databases. Example: SELECT * from
i have the following sql query select * from tblArea where AreaDescription in ('Processing1','Geology
I need help converting the following SQL query into LINQ: select s.teacherid,t.lastname,t.firstname,t.title,t.grade, count(s.TeacherID) from
I have the following SQL query: select ID, COLUMN1, COLUMN2 from (select ID, COLUMN1,
I need the following sql query converting to Propel: SELECT tag.tag, count( content_tag.tag_id )
I have the following SQL query - SELECT * FROM dbo.LocalContacts WHERE name LIKE
I have the following SQL query in oracle: SELECT * FROM ( SELECT s.singleid,s.titel,a.naam,s.taal,SUM(b.aantal)
I am currently using the following SQL Query: (SELECT * FROM `form` WHERE '1'
I have the following query in sql: SELECT a1 FROM dbo.myProductNames WHERE keycode =
I need an sql query to give me the following: All the values in

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.