select查询在特定日期输出每个basestation_id的值,由avg排序。我想从Mysql查询结果中显示php中的表,如下所示。
basestation_id 12-Jun-18 13-Jun-18 14-Jun-18 15-Jun-18 16-Jun-18
5 value1 value2 value3 value4 value5
3 value1 value2 value3 value4 value5
4 value1 value2 value3 value4 value5
1 value1 value2 value3 value4 value5
2 value1 value2 value3 value4 value5
但代码显示结果为
date 12-Jun-18 12-Jun-18 12-Jun-18 12-Jun-18 13-Jun-18 ...
basetstaion_id 5 3 4 1 2 ...
values value1 value1 value1 value1 value2 ...
PHP: -
<?
foreach($result as $key => $row) {
foreach($row as $field => $value) {
$recNew[$field][] = $value;
}
}
//This creates a new array composed/transposed with the field names as keys
//and the "rowed" values as sub-arrays.
echo "<table>\n";
foreach ($recNew as $key => $values) // For every field name (basestation_id,
// , dates, values)
{
echo "<tr>\n"; // start the row
echo "\t<td>" . $key . "</td>\n" ; // create a table cell with the field
name
foreach ($values as $cell) // for every sub-array iterate through all
values
{
echo "\t<td>" . $cell . "</td>\n"; // write cells next to each other
}
echo "</tr>\n"; // end row
}
echo "</table>";
?>
示例数据: -
delivery_date col_head basestation_id receptions daily_avg_stats
16-Jun-18 Sat 16 Jun 152 0 under_avg
16-Jun-18 Sat 16 Jun 13 0 under_avg
16-Jun-18 Sat 16 Jun 140 0 under_avg
16-Jun-18 Sat 16 Jun 12 1093 under_avg
16-Jun-18 Sat 16 Jun 9 0 under_avg
16-Jun-18 Sat 16 Jun 1 50 under_avg
16-Jun-18 Sat 16 Jun 5 250 under_avg
. . . .
15-Jun-18 Fri 15 Jun 152 0 under_avg
15-Jun-18 Fri 15 Jun 13 0 under_avg
15-Jun-18 Fri 15 Jun 140 0 under_avg
15-Jun-18 Fri 15 Jun 12 1734 under_avg
15-Jun-18 Fri 15 Jun 9 0 under_avg
15-Jun-18 Fri 15 Jun 1 0 under_avg
SQL查询以&#34;在avg&#34;下显示结果首先列出的值: -
SELECT
*
FROM
(SELECT
DATE(r.ad_date) AS delivery_date,
DATE_FORMAT(r.ad_date, '%a %d %b') AS col_head,
r.basestation_id,
r.receptions,
(CASE
WHEN r.receptions > FLOOR(avg.monthly_receptions_avg * 1.3) THEN 'over_avg'
WHEN r.receptions < FLOOR(avg.monthly_receptions_avg * 0.7) THEN 'under_avg'
END) AS daily_avg_stats
FROM
receptions r
JOIN receptions_avg avg ON r.basestation_id = avg.basestation_id
WHERE
r.ad_date > DATE_SUB(CURDATE(), INTERVAL 30 DAY)
AND avg.ad_date = CURDATE() - INTERVAL 1 DAY
ORDER BY basestation_id , delivery_date) abc
ORDER BY delivery_date DESC , FIELD(abc.daily_avg_stats, 'under_avg') DESC
答案 0 :(得分:0)
使用此代码
<?
foreach($result as $key => $row) {
foreach($row as $field => $value) {
$recNew[$field][] = $value;
}
}
//This creates a new array composed/transposed with the field names as keys
//and the "rowed" values as sub-arrays.
echo "<table>";
foreach ($recNew as $key => $values) // For every field name (basestation_id,
// , dates, values)
{
echo "<tr>"; // start the row
echo "<td>" . $key . "</td>" ; // create a table cell with the field
name
foreach ($values as $cell) // for every sub-array iterate through all
values
{
echo "<td>" . $cell . "</td>"; // write cells next to each other
}
echo "</tr>"; // end row
}
echo "</table>";
?>