如何结合两个结果?

时间:2014-09-23 16:27:42

标签: php mysql row

如何在同一行显示这两个查询的结果。像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.| Player | 17/8 (8 is result from second query)

这是组合(第一个查询中的新列除以“/”):

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

1 个答案:

答案 0 :(得分: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)))