select查询输出每个基站id在特定日期的值,按平均值排序。我想用php显示mysql查询结果中的表,如下所示。
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>";
?>
样品data:-
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查询以显示列出了“under avg”值的结果first:-
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
1条答案
按热度按时间euoag5mw1#
使用此代码