2014-09-23 151 views
0

我如何顯示這兩個查詢在同一行的結果。事端如17/8(殺死/死亡)。 我有兩個疑問,第一:如何結合兩個結果?

$result1 = mysql_query("SELECT PlayerTable.Player AS 'Player', COUNT(CASE WHEN PlayerTable.InitiatorGroupCat = 'AIRPLANE' THEN PlayerTable.InitiatorGroupCat END) AS Airplane 
FROM 
(
    SELECT HitTbl.InitiatorPlayer AS 'Player', DeadTbl.InitiatorGroupCat 
    FROM 
     (
     SELECT * 
     FROM stats 
     WHERE (EVENT = 'S_EVENT_PILOT_DEAD') 
     AND InitiatorGroupCat != '' 
     ) AS DeadTbl 
    JOIN 
     (
     SELECT * 
     FROM stats 
     WHERE EVENT = 'S_EVENT_HIT' 
     ) AS HitTbl 
    ON DeadTbl.InitiatorID = HitTbl.TargetID 
    WHERE HitTbl.Time IN 
     (
     SELECT MAX(TIME) 
     FROM stats 
     WHERE EVENT = 'S_EVENT_HIT' 
     GROUP BY TargetID 
     ) 
    GROUP BY DeadTbl.InitiatorID,DeadTbl.Time 
    ORDER BY DeadTbl.Time ASC 
) AS PlayerTable 
GROUP BY PlayerTable.Player 
ORDER BY Airplane DESC 
"); 

//return the array and loop through each row 
$i = 1; 
while ($row2 = mysql_fetch_assoc($result1)) 
{ 
?> 
    <tr valign="top"> 
    <td align=center><?php echo $i ++; ?>.</td> 
    <td>   <?php echo $row2['Player'];?></td> 
    <td align=center><?php echo $row2['Airplane'];?></td> 
    </tr> 

第二個查詢:

$result2 = mysql_query("SELECT InitiatorPlayer, COUNT(CASE WHEN InitiatorGroupCat = 'AIRPLANE' THEN InitiatorGroupCat END) AS Deaths 
FROM stats 
WHERE (EVENT = 'S_EVENT_DEAD' OR EVENT = 'S_EVENT_PILOT_DEAD' OR EVENT = 'S_EVENT_EJECTION') 
AND InitiatorGroupCat != '' 
GROUP BY InitiatorPlayer 
ORDER BY Airplane DESC");  

展位查詢工作,但我需要在從排在第一第二的形式顯示查詢結果: 1 |玩家| 17/8(8是從第二查詢結果) 這是cmbination(在由 「/」 分隔第一查詢新列):

<?php echo $row2['Airplane']. "/".$row2['Deaths'];?> 
+0

爲什麼不只是加入第二個查詢,無論您的主鍵是在表中? – 2014-09-23 16:40:55

+0

使用'UNION ALL' – gr3g 2014-09-23 18:55:20

回答

0

以這種方式解決:

<?php 
$result1 = mysql_query("SELECT PlayerTable.Player AS 'Player', COUNT(CASE WHEN PlayerTable.InitiatorGroupCat = 'AIRPLANE' THEN PlayerTable.InitiatorGroupCat END) AS Airplane 
FROM 
(
    SELECT HitTbl.InitiatorPlayer AS 'Player', DeadTbl.InitiatorGroupCat 
    FROM 
     (
     SELECT * 
     FROM stats 
     WHERE (EVENT = 'S_EVENT_PILOT_DEAD' OR EVENT = 'S_EVENT_DEAD') 
     AND InitiatorGroupCat != '' 
     ) AS DeadTbl 
    JOIN 
     (
     SELECT * 
     FROM stats 
     WHERE EVENT = 'S_EVENT_HIT' 
     ) AS HitTbl 
    ON DeadTbl.InitiatorID = HitTbl.TargetID 
    WHERE HitTbl.Time IN 
     (
     SELECT MAX(TIME) 
     FROM stats 
     WHERE EVENT = 'S_EVENT_HIT' 
     GROUP BY TargetID 
     ) 
    GROUP BY DeadTbl.InitiatorID,DeadTbl.Time 
    ORDER BY DeadTbl.Time ASC 
) AS PlayerTable 
GROUP BY PlayerTable.Player 
ORDER BY Airplane DESC 
"); 
$result2 = mysql_query("SELECT InitiatorPlayer, COUNT(CASE WHEN InitiatorGroupCat = 'AIRPLANE' THEN InitiatorGroupCat END) AS Deaths 
    FROM stats 
    WHERE (EVENT = 'S_EVENT_PILOT_DEAD' OR EVENT = 'S_EVENT_DEAD') 
    AND InitiatorGroupCat != '' 
    GROUP BY InitiatorPlayer"); 
//return the array and loop through each row 
//return the array and loop through each row 
$i = 1; 
while (($row2 = mysql_fetch_array($result1)) && ($row1 = mysql_fetch_array($result2))) 
{ 
?> 
    <tr valign="top"> 
    <td align=center><?php echo $i ++; ?>.</td> 
    <td>   <?php echo $row2['Player'];?></td> 
    <td align=center><?php echo $row2['Airplane']. "/" .$row1['Deaths'];?></td> 
    </tr> 

這是一個關鍵:

while (($row2 = mysql_fetch_array($result1)) && ($row1 = mysql_fetch_array($result2)))