2013-08-05 62 views
1

我有使用UNIONPHP聯盟 - 顯示數據僅當表名= table1的

$sql3=" 
    SELECT 
    sequence, 
    ticket_seq, 
    notes as displaydata, 
    datetime as timestamp, 
    updatedby, 
    CONCAT('<strong>Time Start: </strong>',timestart,' - <strong>Time End: </strong>',timeend) as timestartend 
    from ticket_updates where ticket_seq = '".$_GET["seq"]."' 

    UNION 

    SELECT 
    sequence, 
    ticket_seq, 
    description as displaydata, 
    datetime as timestamp, 
    changed_by as updatedby, 
    blankfield 
    from ticket_changes where ticket_seq = '".$_GET["seq"]."' 

    ORDER by timestamp ASC "; 

    $rs3=mysql_query($sql3,$conn) or die(mysql_error()); 
    while($result3=mysql_fetch_array($rs3)) 
    { 
     $timestartend2 = ($result3["timestartend"] > '') ? '<br><br>'.$result3["timestartend"] : ''; 

     $sql9="SELECT *, TIMEDIFF(timeend, timestart) AS support_time_used FROM ticket_updates WHERE sequence = '".$result3["sequence"]."' "; 
     $rs9=mysql_query($sql9,$conn) or die (mysql_error()); 
     $result9=mysql_fetch_array($rs9); 
     $update_times = 'Time Took: '.$result9["support_time_used"]; 

     echo '<tr> 
        <td colspan="2" bgcolor="#666666" align="left"><font color="#FFFFFF"><strong>'.$result3["updatedby"].'</strong></font></td> 
        <td bgcolor="#666666" align="right"><font color="#FFFFFF"><strong>'.$result3["timestamp"].'</strong></font></td> 
       </tr> 
       <tr> 
        <td colspan="3">'.nl2br(stripslashes($result3["displaydata"])).''.$timestartend2.'<br><br>'.$update_times.'</td> 
       </tr> 
       <tr> 
        <td colspan="3"></td> 
       </tr>'; 
    } 

while循環中還有另外一個SQL查詢($sql9)該發現的時間差從2個表選擇該PHP代碼但如果結果/行來自名爲ticket_updates的表而不是ticket_changes表,則我只需要它顯示該數據。

我該如何做到這一點?

+0

爲什麼工會爲什麼不離開加入,如果你必須顯示'ticket_updates'的數據,如果你明白我的意思 –

回答

1

可能有更好的方法來做到這一點,但我認爲你可以改變你的查詢並檢查數據來自哪個表,你將在結果中得到一個新列('rowTable'),那麼在你的代碼中你可以使用它:

$sql3=" 
SELECT 
'ticket_updates' as rowTable, 
sequence, 
ticket_seq, 
notes as displaydata, 
datetime as timestamp, 
updatedby, 
CONCAT('<strong>Time Start: </strong>',timestart,' - <strong>Time End: </strong>',timeend) as timestartend 
from ticket_updates where ticket_seq = '".$_GET["seq"]."' 

UNION 

SELECT 
'ticket_changes' as rowTable, 
sequence, 
ticket_seq, 
description as displaydata, 
datetime as timestamp, 
changed_by as updatedby, 
blankfield 
from ticket_changes where ticket_seq = '".$_GET["seq"]."' 

ORDER by timestamp ASC "; 

    $rs3=mysql_query($sql3,$conn) or die(mysql_error()); 
while($result3=mysql_fetch_array($rs3)) 
{ 
    $timestartend2 = ($result3["timestartend"] > '') ? '<br><br>'.$result3["timestartend"] : ''; 
if ($result3['rowTable'] == 'ticket_updates') { 
    $sql9="SELECT *, TIMEDIFF(timeend, timestart) AS support_time_used FROM ticket_updates WHERE sequence = '".$result3["sequence"]."' "; 
    $rs9=mysql_query($sql9,$conn) or die (mysql_error()); 
    $result9=mysql_fetch_array($rs9); 
    $update_times = 'Time Took: '.$result9["support_time_used"]; 
} else { 
    $update_times = 'N/A'; 
} 
    echo '<tr> 
       <td colspan="2" bgcolor="#666666" align="left"><font color="#FFFFFF"><strong>'.$result3["updatedby"].'</strong></font></td> 
       <td bgcolor="#666666" align="right"><font color="#FFFFFF"><strong>'.$result3["timestamp"].'</strong></font></td> 
      </tr> 
      <tr> 
       <td colspan="3">'.nl2br(stripslashes($result3["displaydata"])).''.$timestartend2.'<br><br>'.$update_times.'</td> 
      </tr> 
      <tr> 
       <td colspan="3"></td> 
      </tr>'; 
}