PHP Union - 仅当表名 = table1 时显示数据

标签 php mysql

我有这个 PHP 代码,它使用 UNION 从 2 个表中进行选择

$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)找到时差,但如果结果/行来自名为 ticket_updates 的表,我只需要它来显示该数据而不是 ticket_changes表。

我怎样才能做到这一点?

最佳答案

可能有更好的方法,但我认为您可以更改查询并检查数据来自哪个表,您将在结果中获得一个新列(“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>';
}

关于PHP Union - 仅当表名 = table1 时显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18056231/

相关文章:

php - 如何通过结束 session 从站点注销

php - Paypal 快速结帐 session 已过期/ token 无效 - 错误 10411

mysql - 统计今天、昨天……以及其他时间添加的行数

php - 转义 MySQL 语句的字符串

javascript - 如何通过ajax将js变量传递给php

php - 如何在完成一天后自动更新数据(00 :00) in mysql

php - wordpress wp 编辑器,将数据插入数据库(安全方式)

mime - 语法错误,意外的 T_SL

javascript - 谷歌分析,用户

php - 迭代 SimpleXml xpath 结果