我需要构建下表。
<style type="text/css">
.tg {border-collapse:collapse;border-spacing:0;}
.tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
.tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
.tg .tg-baqh{text-align:center;vertical-align:top}
.tg .tg-lap0{font-size:100%;text-align:center;vertical-align:top}
.tg .tg-yw4l{vertical-align:top}
</style>
<table class="tg" style="undefined;table-layout: fixed; width: 593px">
<colgroup>
<col style="width: 67px">
<col style="width: 72px">
<col style="width: 116px">
<col style="width: 116px">
<col style="width: 116px">
<col style="width: 106px">
</colgroup>
<tr>
<th class="tg-lap0">State</th>
<th class="tg-baqh">City</th>
<th class="tg-baqh">Three Days Ago</th>
<th class="tg-baqh">Two Days Ago</th>
<th class="tg-baqh">One Day Ago</th>
<th class="tg-baqh">Total</th>
</tr>
<tr>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
</tr>
<tr>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
</tr>
<tr>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
</tr>
<tr>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
</tr>
</table>
其中state和city是简单的选择,但 three_days_ago、two_days_ago、one_day_ago 是相同城市名称的 DB 条目计数。所有这些都在数据库的同一张表中。
所以我把我的查询想象成这样:
$union = pg_query($db,
"
(
SELECT estado, municipio, COUNT(*)
FROM focos_bdq
WHERE bioma LIKE 'Amazônia'
AND satelite LIKE 'AQUA_M-T'
AND data_hora_gmt::date='$three_days_ago'
GROUP BY municipio, estado
ORDER BY COUNT(*) DESC
)
UNION ALL
(
SELECT estado, municipio, COUNT(*)
FROM focos_bdq
WHERE bioma LIKE 'Amazônia'
AND data_hora_gmt::date='$two_days_ago'
GROUP BY municipio, estado
ORDER BY COUNT(*) DESC
)
UNION ALL
(
SELECT estado, municipio, COUNT(*)
FROM focos_bdq
WHERE bioma LIKE 'Amazônia'
AND data_hora_gmt::date='$one_day_ago'
GROUP BY municipio, estado
ORDER BY COUNT(*) DESC
)
"
但是当我需要将日期放在同一行时,这个查询会导致一个非常长的表。