计数mysql联合查询时显示不同的文本

时间:2018-09-11 07:10:23

标签: php

我正在使用以下代码获取 2列中的计数-重新尝试日期和保留日期

{{1}}

我得到如下结果,在这前6个值来自 reattemptdate 列,接下来的6个值来自 holddate 列,如何显示文本Hold而不是在最后6个重新尝试图片下方的行:

enter image description here

1 个答案:

答案 0 :(得分:2)

您可以根据“ Held”列使用逻辑: 例如:

...
foreach ($results as $result) 
{
    if ($result['Held']==0){
      $header = "Reattemptdate";
    } else {
      $header = "holddate";
    }
    echo $header.": ".$result['count']."<br>";
}

或在结果集中添加一个显式标头,如下所示:

<?php
$sql = "SELECT employeename, DATE(reattemptdate) as date, 
COUNT(*) as count, 0 as Held, 'reattemptdate' as header FROM orders 
WHERE DATE(reattemptdate)  = CURDATE() 
GROUP BY employeename, date

UNION

SELECT employeename, DATE(holddate) as date, 
COUNT(*) as count, 1 as Held, 'holddate' as header  FROM orders 
WHERE DATE(holddate)  = CURDATE() 
GROUP BY employeename, date
"; 
$results = $db_handle->runSelectQuery($sql); 
$numrowsresult =$results[0]['count'];

foreach ($results as $result) 
{
    echo $result['header'].": ".$result['count']."<br>";
}