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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:23:22+00:00 2026-05-30T13:23:22+00:00

Basically, what I want to do is to get all children in another table

  • 0

Basically, what I want to do is to get all children in another table within a certain birth year. So I have two tables. Let’s say it’s a school table

School

school_id
child_id

Children

child_id
birth_year
name
etc

My first attempt is to use subquery, which is something like this

SELECT *, (SELECT COUNT(*) FROM school LEFT JOIN children ON school.child_id = children.child_id) as total FROM school LEFT JOIN children ON school.child_id = children.child_id GROUP BY birth_year

The problem with this query is the subquery will run all throughout the records, so if I have 1000 records, I think the query (and the subquery) will run 1000 times before grouping by birth_year, which is slow, it’s almost 3-5 seconds for 500 sample data.

So to optimize it, I’m doing this.

Recursive Query Using PHP

First, get the distinct birth year of ALL children who are in school. So I’m gonna query something like

SELECT birth_year FROM school LEFT JOIN children ON school.child_id = children.child_id

It will return data like

birth_year
==========
2009
2010
2011

Which I’m gonna use in another query in PHP (let’s say I store the result in $row variable)

foreach ($row as $r){
    $new_array[] = count($this->db->get_child_data($r->birth_year)); //this is pseudocode only, to get the number of children data who have birth_year of 2009-2011
}

Though it will run additional three queries, this is really fast as the count is simple. It only takes less than 0.5 seconds for 500 sample data.

However, I’m wondering if there’s any way of optimizing it? Or better, is there a way to do it in a single query with similar performance?

I’m trying to do this, but it ends up super slow and crashes my WAMP.

SELECT * FROM children WHERE birth_year IN (SELECT GROUP_CONCAT(DISTINCT birth_year) FROM school LEFT JOIN children ON school.child_id = children.child_id )

The subquery

SELECT GROUP_CONCAT(DISTINCT birth_year) FROM school LEFT JOIN children ON school.child_id = children.child_id

when run separately correctly and quickly returns

2009,2010,2011

And when I query

SELECT * FROM children WHERE birth_year IN (2009,2010,2011)

It also works fast , so I’m quite confused why when I join both queries, it is slow to the point to crash my WAMP.

Sorry for the long post and thanks in advance

  • 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-30T13:23:23+00:00Added an answer on May 30, 2026 at 1:23 pm

    The problem is that using subqueries in the IN or SELECT clauses often causes them to be run again for each row in the outer query. To avoid this, try joining to the subquery. This should result in the subquery being run only once, and cached.

    SELECT c.* 
    FROM children c, (SELECT birth_year FROM school LEFT JOIN children ON school.child_id = children.child_id GROUP BY birth_year) b
    WHERE c.birth_year = b.birth_year
    

    That said, it looks like you’re just getting every child in any school anyway, so a simpler JOIN might also give you the same result.

    SELECT c.* 
    FROM children c, school s
    WHERE c.child_id = s.child_id
    

    If you just want to get the count of kids in each school, for each birth_year

    SELECT count(c.child_id), s.* 
    FROM school s 
    LEFT JOIN children c ON c.child_id = s.child_id
    GROUP BY s.school_id, c.birth_year
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, I want to get all rows from the customers table that do NOT
I have a table with two columns: latitude and longitude. I want to get
Basically, I want to get a list of all installed apps and pick one
I have an SPListItemCollection. I basically want to get one of the items (randomly)
So, basically I have an NSArray . I want to get an array with
I want to get all of the Brushes, this is basically pseudocode which will
Have 4 relationship tables in my database and want to join them to get
basically i just want to get all the text after the _ . i
Basically I want to get the value of a textbox which is to select
What i basically want to do is to get content from a website and

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.