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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:10:15+00:00 2026-06-13T07:10:15+00:00

Is it possible build an array of SQL commands to query the DB? What

  • 0

Is it possible build an array of SQL commands to query the DB? What I have is three tables, each have columns with amounts due. Would like to select anything that is due and display on screen so it can be invoiced (preferably in a table) and each row with it’s respective customers dues.

I can select everything that is due using UNION ALL between the three tables, however I cant figure out how to list them by ID in the table row.

Below is what I have so far. At this pace I’ll have to run each query separately and list them in three separate lists. Suggestions?

      <table>
        <tr>
            <th> ID</th>
            <th> Cost 1</th>
            <th> Cost 2</th>
            <th> Cost 3</th>
        </tr>

      <?php 
    $list1 = "SELECT ID, Cost FROM Table1 WHERE Invoiced IS NULL;";
    //$list2 = "SELECT ID, Price2 FROM Table2 WHERE Expiration BETWEEN '$curDate' AND '$maxDate';";
    //$list3 = "SELECT ID, Price3 FROM Table3 WHERE Expiration BETWEEN '$curDate' AND '$maxDate'";

    $result = mysql_query($list1, $link) or die(mysql_error());

    $num_rows = mysql_num_rows($result);
    $num_fields = mysql_num_fields($result);

    for ($i=0; $i<$num_rows; $i++) {
        for ($j=0; $j<$num_fields; $j++) {
            $invoice[$i][mysql_fieldname($result,$j)] = mysql_result($result,$i,mysql_field_name($result,$j));
            }
        }

    //eventually the order it should be listed on screen
    for($i=0; $i<count($invoice); $i++) {
        echo "<tr><td>".$invoice[$i]["ID"]."</td>
        <td>".$invoice[$i]["Cost"]."</td>
        <td>".$invoice[$i]["Price2"]."</td>
        <td>".$invoice[$i]["Price3"]."</td></tr>";
    }

      ?>
      </table>

Edit after comment:

Query being passed and returning syntax error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'all LEFT JOIN table1 ON all.ID = table1.ID LEFT JOIN t' at line 7:

    $query = "
    SELECT all.ID, table1.Cost1, table2.Price2, tabl3.Price3
    FROM 
        (SELECT ID, Cost1 FROM table1 WHERE Invoiced IS NULL
            UNION
        SELECT ID, Price2 FROM table2 WHERE Expiration BETWEEN '$curDate' AND '$maxDate'
            UNION
        SELECT ID, Price3 FROM table3 WHERE Expiration BETWEEN '$curDate' AND '$maxDate') AS all
    LEFT JOIN table1 ON all.ID = table1.ID
    LEFT JOIN table2 ON all.ID = table2.ID
    LEFT JOIN table3 ON all.ID = table3.ID
    ";
  • 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-13T07:10:16+00:00Added an answer on June 13, 2026 at 7:10 am

    From the table header you have created above which places the three Cost columns in a single row by ID, you seem to imply that you want to to JOIN the three tables together on their ID. I am using a LEFT JOIN here, to be sure that all rows from Table1 are present, even if there is no corresponding row in either of the other two tables.

    SELECT
      Table1.ID,
      Table1.Cost as Cost1,
      Table2.Price2 AS Cost2,
      Table3.Price3 AS Cost3
    FROM
      Table1 
      LEFT JOIN Table2 ON Table1.ID = Table2.ID
      LEFT JOIN Table3 ON Table1.ID = Table3.ID
    WHERE 
      Table1.Invoiced IS NULL
      AND Table2.Expiration BETWEEN '$curDate' AND '$maxDate'
      AND Table3.Expiration BETWEEN '$curDate' AND '$maxDate'
    

    Update after comments:

    In the case that Table2 may have an ID not held in Table1 or Table3, for example (where Table1.ID can’t be considered authoritative), you can get the total set of DISTINCT ID from all 3 tables via a UNION and use that to join against:

    SELECT
      allID.ID,
      Table1.Cost1,
      Table2.Price2 AS Cost2,
      Table2.Price3 AS Cost3
    FROM
      /* Subquery gets a distinct set of IDs from all tables via UNION 
         so the outer query has a complete list to join against the other tables */
      (
        SELECT ID FROM Table1
        UNION SELECT ID FROM Table2
        UNION SELECT ID FROM Table3
      ) allID
      LEFT JOIN Table1 ON allID.ID = Table1.ID
      LEFT JOIN Table2 ON allID.ID = Table2.ID
      LEFT JOIN Table3 ON allID.ID = Table3.ID
    /* Sorry, forgot the WHERE clause here */
    WHERE
      Table1.Invoiced IS NULL
      AND Table2.Expiration BETWEEN '$curDate' AND '$maxDate'
      AND Table3.Expiration BETWEEN '$curDate' AND '$maxDate'
    

    Note that the existence of three tables with nearly identical column structures in a one-to-one relationship probably implies a design problem. You might consider combining these into a single table.

    A further note about the PHP:

    In PHP, we almost never use an incremental for loop for iteration as you would in C/C++. Instead, we typically make use of a foreach or when fetching rows from a query, a while loop.

    // Fetch in a while loop
    $invoice = array();
    // $result is your query resource as you already have it...
    while ($row = mysql_fetch_assoc($result)) {
      // Accumulate rows into $invoice array
      $invoice[] = $row;
    }
    // Then loop over the array:
    foreach ($invoice as $inv) {
      echo "<tr>
        <td>{$inv['ID']}</td>
        <td>{$inv['Cost1']}</td>
        <td>{$inv['Cost2']}</td>
        <td>{$inv['Cost3']}</td>
      </tr>"; 
    }
    

    Final update:

    Yes, the WHERE clause will restrict for all conditions met. If you need to limit them individually, you must do so in subqueries which are then joined together, using the same UNION subquery to get the distinct set of ID

    SELECT
      allID.ID,
      T1.Cost1,
      T2.Price2 AS Cost2,
      T3.Price3 AS Cost3
    FROM
      (
        SELECT ID FROM Table1
        UNION SELECT ID FROM Table2
        UNION SELECT ID FROM Table3
      ) allID
      LEFT JOIN (SELECT ID, Cost AS Cost1 FROM Table1 WHERE Invoiced IS NULL) T1 ON allID.ID = T1.ID
      LEFT JOIN (SELECT ID, Price2 AS Cost2 FROM Table2 WHERE Expiration BETWEEN '$curDate' AND '$maxDate') T2 ON allID.ID = T2.ID
      LEFT JOIN (SELECT ID, Price3 AS Cost3 FROM Table3 WHERE Expiration BETWEEN '$curDate' AND '$maxDate') T3 ON allID.ID = T3.ID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of 16 squares and I would like to auto complete
I have an array that I would like to sort, that should be easy,
Is it possible to have a single sql query return a nested structure rather
I would like to build a function that takes a multidimensional array and prints
Would it be possible to build and deploy Meteor applications on Google App Engine
If I have 6 modules in my project is it possible to build only
Using the Kohana query builder, is it possible to build my query piece by
Possible Duplicate: Permutations - all possible sets of numbers I have an array that
This is based in PHP. Is it possible to take an array like this:
Is it possible to build balancer trees with several levels in mod_jk configuration? for

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.