在表行中显示多个数据行

时间:2017-05-11 01:37:47

标签: mysql

感谢早先的例子 Vara Prasad ,我已经扩展了他的 使用MySQL GROUP_CONCAT 的初步示例,并且肯定已经学会了 从那以后。

以下MySQL是我目前使用的并且正在运行 - 下面 是一个结果的例子,虽然接近下面的期望输出 没有实现所需的数据显示 - 它只显示第一个 LOCATION 字段。我觉得也许MySQL语法变得笨拙: (注意:同样在我的示例中,我在鼠标悬停时使名称字段突出显示,并且可单击以按客户端搜索数据库名称

注意: PAX =乘客。 TotalPAX =成人和总人数的总和儿童乘客。 TTime = TransferTime。 LOCATION和DESTINATION可能会有所不同,但他们的TRANSFER REF将是相同的。

$query = mysql_query("SELECT *, SUM(PAX) AS TotalPAX, GROUP_CONCAT('<a 
href=', 'PrintTransfers.php?Search=',TransferRef,'>', '<div class=', 
'TABLE-ROW-SELECTED', '>', Name, ' | ', PAX, '</div></a>' SEPARATOR '<hr 
/>') AS PasName FROM Transfers WHERE Date = '$DateToday' AND Agency = 'BT-
TAXIS' GROUP BY TransferRef, TTime ASC, Agency ORDER BY ServiceType DESC, 
TTime ASC");
$Ref = 1;
while ($row = mysql_fetch_array($query))
{
  $ID              = $row['ID']; // Internal Use
  $TTime           = $row['TransferTime'];
  $Location        = $row['PasLocation'];  
  $Destination     = $row['Destination'];
  $Name            = $row['PasName'];
  $PAX             = $row['TotalPAX']; // Stored as eg 2.3 which represents 
  2 Adults + 3 Children
  $PASSENGERS      = explode(".", $PAX);
  // var_dump($PASSENGERS);
  $Adults          = $PASSENGERS[0];
  $Children        = $PASSENGERS[1];
  $PAX             = $Adults + $Children;
  $TransferRef     = strtoupper($row['TransferRef']);     

// ADD HIGHLIGHTS      
switch ($Status){
case "GREY":
  $StatusHighlight = "#CCCCCC";
break;

default:
  $StatusHighlight = "#FFFFFF";    
}

print "<tr>
  <td bgcolor='$StatusHighlight' style='font-size:12px; text-
  align:center;'>$Ref</td>
  <td bgcolor='$StatusHighlight' style='font-size:12px; text-
  align:center;'>$TTime</td>
  <td bgcolor='$StatusHighlight' style='font-size:12px; text-
  align:center;'>$Location</td>
  <td bgcolor='$StatusHighlight' style='font-size:12px; text-
  align:center;'>$Destination</td>      
  <td bgcolor='$StatusHighlight' style='white-space: nowrap; font-size:12px; 
  text-align:right;'>$Name</td>     
  <td bgcolor='$StatusHighlight' style='font-size:12px; text-
  align:center;'>$PAX</td>
  <td bgcolor='$StatusHighlight' style='font-size:12px; text-
  align:center;'>$TransferRef</td>
</tr>";
$Ref++; 
} // END WHILE

电流输出: enter image description here

下表是理想的结果 - 注意&#34; T-TIME&#34;和&#34;转移 REF&#34;分组,但&#34; LOCATION&#34;和&#34;目的地&#34;不是 - T-TIME 是 ORDERED ASC:

期望的输出: enter image description here

此外,以下MySQL确实正确地对结果进行了排序,但是将每个条目显示为不需要的完整行,并使用&#34; GROUP BY&#34;没有达到要求的结果:

$query = mysql_query("SELECT * FROM Transfers WHERE Date = '$DateToday' 
AND Agency = 'BT-TAXIS' ORDER BY ServiceType DESC, TransferRef, TTime 
ASC");

**GROUP BY** and **GROUP_CONCAT** seem to have an adverse affect on the data output and is not very flexible so I am thinking perhaps I should be working more with PHP than with MySQL to achieve the desired output.

我已经在Stack和Google上多次通过所有相似的例子,所以我的问题是如何用PHP和多个MySQL调用以编程方式对数据进行排序,如果这确实是正确的选择路径。关心所有可能考虑这个问题的人。

仍然使用这个我改变了MySQL查询,添加了一个数组,现在尝试了以下...没有错误:

$GetName = "PasName";
$query = mysql_query("SELECT *, GROUP_CONCAT('<a href=', 'SearchName.php?
Search=',ID,'>', Name, ' @ ', PAX, '</a>',  ' @ ', Location, ' @ ' SEPARATOR 
' | ') AS PasName FROM Transfers WHERE Date = '$DateToday' AND Agency = 'BT-
TAXIS' GROUP BY TransferRef, TTime ASC, Agency ORDER BY ServiceType DESC, 
TTime ASC");

$Ref = 1;   
while ($row = mysql_fetch_array($query))
{
   $ID     = $row['ID'];
   $TTime  = $row['TransferTime'];
   $Agency = $row['Agency'];
   $Name   = $row[$GetName];

if (strpos($Name, ' | ') !== false) // ADDED TO INDICATE MULTIPLE ROWS
{
  $Name = str_replace(' | ', '', $Name); // THIS WORKS
  $Names = explode(" @ ", $Name); // @ FROM MySQL QUERY - ADDED TO MARK 
DIFFERENT FIELDS
  $Name = str_replace(' @ ', '', $Name); // THIS DOESN'T WORK
// var_dump($Names);

  for($i = 0, $l = count($array); $i < $l; ++$i) {
    $Name     = trim($Names[$i]);  
    $PAX      = trim($Names[$i]);
    $Location = trim($Names[$i]);     
  }
} else {    
   $Names = explode(" @ ", $Name);   
// var_dump($Names);
   $Name = trim($Names[0]);
   $PAX = trim($Names[1]);         
   $Location = trim($Names[2]);
}

   $Destination = strtoupper($row['Destination']);
   $ServiceType = strtoupper($row['ServiceType']);
   $TransferRef = $row['Coupon'];

print "<tr>
  <td bgcolor='$StatusHighlight' style='font-size:12px; text-
  align:center;'>$Ref</td>
  <td bgcolor='$StatusHighlight' style='font-size:12px; text-
  align:center;'>$TTime</td>
  <td bgcolor='$StatusHighlight' style='font-size:12px; text-
  align:center;'>$Location</td>
  <td bgcolor='$StatusHighlight' style='font-size:12px; text-
  align:center;'>$Destination</td>      
  <td bgcolor='$StatusHighlight' style='white-space: nowrap; font-size:12px; 
  text-align:right;'>$Name</td>     
  <td bgcolor='$StatusHighlight' style='font-size:12px; text-
  align:center;'>$PAX</td>
  <td bgcolor='$StatusHighlight' style='font-size:12px; text-
  align:center;'>$TransferRef</td>
</tr>";
$Location = ""; // WITHOUT THIS LAST ENTRY IS REPEATED
$Ref++;
} // END WHILE

数据在那里,它只是不在正确的地方 - 任何想法?这是现在的输出: enter image description here

0 个答案:

没有答案