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
";
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 toJOINthe three tables together on theirID. I am using aLEFT JOINhere, to be sure that all rows fromTable1are present, even if there is no corresponding row in either of the other two tables.Update after comments:
In the case that
Table2may have anIDnot held inTable1orTable3, for example (whereTable1.IDcan’t be considered authoritative), you can get the total set ofDISTINCT IDfrom all 3 tables via aUNIONand use that to join against: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
forloop for iteration as you would in C/C++. Instead, we typically make use of aforeachor when fetching rows from a query, awhileloop.Final update:
Yes, the
WHEREclause 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 sameUNIONsubquery to get the distinct set ofID