我試圖做兩個單獨的數據庫quires並將結果返回給窗體。每個結果都寫入一個表中。結合兩個單獨的MySQL查詢的結果
我希望能夠將兩個查詢合併爲一個,並按任務編號排序結果。
第一個查詢:
//Booking
$Date= date("d/m/Y");
$driver = $_SESSION['username'];
$dbTaskRecords = "SELECT * FROM booking WHERE driver='$driver' AND Date= CAST('$Date_search' AS DATE) ORDER BY TaskNo ASC";
$dbTaskRecords_result = mysql_query($dbTaskRecords);
第二個查詢:
//Return Booking
$dbTaskReturn = "SELECT * FROM returnbooking WHERE driver='$driver' AND Date= CAST('$Date_search' AS DATE) ORDER BY TaskNo ASC";
$dbTaskReturn_result = mysql_query($dbTaskReturn);
結果然後通過while語句輸出到頁。
$i=0;
while ($row = mysql_fetch_array($dbTaskRecords_result)){
//Control Structure for Move Time on first Job of day
if ($i==0){
$time = $row["Time"];
//$time = 'N/A';
}else{
$time = 'N/A';
}
//Get Rego from trucks table
$truckID = $row["TruckID"];
$Rego_select = mysql_query("SELECT VechicleRegistration FROM trucks WHERE TruckID = '$truckID'")
or die("Problem reading table: " . mysql_error());
$Rego = mysql_result($Rego_select,0);
//Get unregisted from trucks table
$Unregisted_select = mysql_query("SELECT Unregistered FROM trucks WHERE TruckID = '$truckID'")
or die("Problem reading table: " . mysql_error());
$Unregisted = mysql_result($Unregisted_select,0);
$id_note = $row["BookingID"];
echo '<td><a href="taskpage.php?id='.$id_note.'"><button>'. $row['TaskNo']."</button><a/></td>";
echo "<td>". $time . "</td>"; // Time Frame
echo "<td>". $Unregisted."</td>"; // Pickup
echo "<td>". $Rego."</td>"; // Unregisted
echo "<td>".$row["PickupLocation"] . "</td>"; // Rego
echo "<td>".$row["DropOffLocation"] . "</td></tr>"; // Delivery
$i=$i+1;//Control Set
}
echo'</tr>';
我重複這個相同的輸出代碼,從返回預訂的結果。
是否可以將兩個查詢合併爲一個,以便兩個表中的結果集可以由ASC進行排序並由上述while語句輸出。
乾杯。我不確定如何將結果輸入變量。我試過$ time = mysql_result($ dbTaskRecords,0);但那不起作用。 – 2011-05-05 10:33:50
@Samuel Meddows - 您應該可以像訪問單個select語句時那樣訪問列。 – Thomas 2011-05-05 15:11:11
如果兩個子查詢都在同一個表上,那麼使用SELECT *沒什麼問題......是嗎? – Rooster242 2014-03-20 20:53:42