PHP Union-仅当table name=table1时显示数据
我有一段PHP代码,它使用一个并集从两个表中进行选择 在while循环中有另一个SQL查询$sql9,它查找时间差,但是如果结果/行来自名为ticket\u updates的表,而不是ticket\u changes表,我只需要它来显示该数据PHP Union-仅当table name=table1时显示数据,php,mysql,Php,Mysql,我有一段PHP代码,它使用一个并集从两个表中进行选择 在while循环中有另一个SQL查询$sql9,它查找时间差,但是如果结果/行来自名为ticket\u updates的表,而不是ticket\u changes表,我只需要它来显示该数据 如何实现这一点?可能有更好的方法,但我认为您可以更改查询并检查数据来自哪个表,您将在结果中获得一个新列“rowTable”,然后在代码中使用它: $sql3=" SELECT 'ticket_updates' as rowTable, sequence,
如何实现这一点?可能有更好的方法,但我认为您可以更改查询并检查数据来自哪个表,您将在结果中获得一个新列“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>';
}
可能有更好的方法,但我认为您可以更改查询并检查数据来自哪个表,您将在结果中获得一个新列“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>';
}
如果您必须显示票证更新中的数据,如果您理解我的意思,为什么联盟为什么不离开加入如果您必须显示票证更新中的数据,如果您理解我的意思,为什么联盟为什么不离开加入