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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:05:08+00:00 2026-05-27T14:05:08+00:00

I have to adapt a PHP-script that is connected to a extremely messy database-design.

  • 0

I have to adapt a PHP-script that is connected to a extremely messy database-design.
The best way to explain the problem is to post the code, within which im going to describe whats happening through comments inside the code-block. Plus a short introduction:

Short Story:
1.) There is a “mainQuery” and inside the for-loop that displays its data there are 3 nested subqueries.
2.) All three subqueries need the main_table_ID in order to count the occurences of each fetched_row.

What i want:
1.) An optimzied mainQuery that counts 3 occurences of an id separately on its own.
2.) Thus no subqueries at all inside the for loop.

What i have already achieved via optimizing the mainQuery:
1.) I can filter a subquery one at a time by, see statement:

SELECT
       main_table.id_col,
       count(table_1.a_col) AS count_1,
       main_table.a_col,
       main_table.b_col,
       main_table.c_col,
       main_table.d_col,
       main_table.e_col

FROM   main_table
LEFT JOIN
       table_1
ON
       table_1.a_col = main_table.id_col
GROUP BY
       main_table.id_col

But when i add another LEFT JOIN for the one of other nested statements inside the for-loop, the statement sums everything up and count_1 to count_3 look the same.

And now the code-block containing the subqueries and the for-loop:

<?php
$mainQuery = "SELECT 
                          main_table.id_col, 
                          main_table.a_Col, 
                          main_table.b_Col, 
                          main_table.c_col, 
                          main_table.d_col, 
                          main_table.e_col 
                   FROM 
                          main_table";

$mainQueryResult = mysql_query($mainQuery);
$mainQueryRows = mysql_num_rows($mainQueryResult);

for($j = 0 ; $j < $mainQueryRows ; ++$j){
    //mainQuery data
    $mainQueryRow = mysql_fetch_row($mainQueryResult);
    $main_table_ID = $mainQueryRow[0];

    //first subquery, can be filtered out one at a time 
    //using introductory-statement
    $count_1_subQuery = "SELECT * FROM table_1 
                         WHERE table_1.id_col = '$main_table_ID'";
    //first subquery-count of rows with matching id
    $count_1 = mysql_num_rows(mysql_query($count_1_SubQuery, $connection));

    //second subquery
    $count_2_subquery = "SELECT * FROM table_2 
             WHERE table_2.id_col = '$main_table_ID'";
    //second subquery-count of rows with matching id
    $count_2 = mysql_num_rows(mysql_query($count_2_subQuery, $connection));

    //third subquery
    $count_3_subquery = "SELECT * FROM table_3 
             WHERE table_3.id_col = '$main_table_ID'";
    //third subquery-count of rows with matching id
    $count_3 = mysql_num_rows(mysql_query($count_3_subQuery, $connection));

    //Fill Table with count_1 to count_3 
        //and main_table.a_col to main_table.e_col
    //no problems here
}
?>

I hope i could explain the problem clearly enough and i also hope one of you guys can help me, if he/she can allot time for such a question.

  • 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-27T14:05:08+00:00Added an answer on May 27, 2026 at 2:05 pm

    First GROUP BY in subqueries, then JOIN to the main table:

    SELECT  
          m  . id_col
        , g1 . count_1  
        , g2 . count_2  
        , g3 . count_3  
        , m  . a_col  
        , m  . b_col  
        , m  . c_col  
        , m  . d_col  
        , m  . e_col      
    FROM 
            main_table AS m
        LEFT JOIN 
            ( SELECT id_col
                  , COUNT(*) AS count_1  
              FROM table_1  
              GROUP BY id_col 
            ) AS g1
          ON g1.id_col = m.id_col
        LEFT JOIN 
            ( SELECT id_col
                   , COUNT(*) AS count_2  
              FROM table_2 
              GROUP BY id_col 
            ) AS g2
          ON g2.id_col = m.id_col
        LEFT JOIN 
            ( SELECT id_col
                   , COUNT(*) AS count_3  
              FROM table_3  
              GROUP BY id_col 
            ) AS g3
          ON g3.id_col = m.id_col
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a really confusing problem. I have a PHP script that polls another
I have a php script that execute a bash script. I try to pass
I have the following script that I need to adapt to read from a
I have a database that has a 'plant' theme. I am wanting a script
I have downloaded a free astrology script in PHP. The script receives some data
I have a homepage that contains a huge amount of php code to populate
Im trying to adapt some code that I have found online that uses this
I have a jquery script that collects data from different sites and saves it
i have to adapt an existing web application for use in a kiosk that
I have got that link: <a href=index.php>Political Science</a> I put it dynamically with php

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.