PHP join two DB2 resource statements

burai

New Member
I have two queries, with the same [group by] fields, but different (sum) calculation fields.I want to either:[*]Add [Query #2] [Field 3] to [Query #1] [Field 4], for every record[*]Create a new array to later retrieve the data, which has [A1], [A2], [A3], [B4].For both of these however, I need to know that records will be in exact same order.. maybe 'select' the record from the 2nd resource statement to join it to the original by all 3 key values.\[code\]$conn_resource = db2_connect ( "*LOCAL", "", "" );$sql1 = "SELECT [A1], [A2], [A3], SUM([A4]) FROM [MyFile] WHERE [G] > 5 GROUP BY [A1], [A2], [A3] ";$stmt1 = db2_prepare ( $conn_resource, $sql1 );$sql2 = "SELECT [B1], [B2], [B3], SUM([B4]) FROM [MyFile] WHERE [G] > 5 GROUP BY [B1], [B2], [B3] ";$stmt2 = db2_prepare ( $conn_resource, $sql2 );if (! db2_execute ( $stmt1 ) || ! db2_execute ( $stmt2 )) { //failure}while ( $row = &db2_fetch_array ( $stmt1 ) ) { $sqlStatementTwoRow = db2_fetch_array ( $stmt2 ); $row[4] = $row[3] + $sqlStatementTwoRow[4];}\[/code\]The above example should work, in theory, but i have no confirmation that the records from $stmt2 come in the exact same order as $stmt1.EditThe answer provided seems to be the solution, the more and more i migrate that solution into my real SQL statement.I will post the full, live SQL which uses this idea, once i get it finished.
 
Top