我有一张桌子node_weather
,看起来像这样:
+--------------------+--------------+------+-----+--------------------+
| Field | Type | Null | Key | |
+--------------------+--------------+------+-----+--------------------+
| W_id | mediumint(9) | NO | PRI | ement |
| temperature | int(10) | YES | | |
| humidity | int(10) | YES | | |
| wind_direction | int(10) | YES | | |
| wind_speed | int(10) | YES | | |
| wet_temperature | int(10) | YES | | |
| P_id | mediumint(9) | YES | MUL | |
| time | timestamp | NO | | CURRENT_TIMESTAMP |
| level_current | int(10) | YES | |
在这个表中,我记录了由P_id字段定义的4个不同的气象站的数据(=> P_id值可以是1、2、3或4)。从所有的台站传来的数据都在不断地填写表格。
如何创建查询以获取每个气象站的最后记录行?
发布于 2014-07-03 06:51:02
也许是这样的:
SELECT
*
FROM
node_weather
JOIN
(
SELECT
tblInner.P_id,
MAX(tblInner.time) AS maxTime
FROM
node_weather as tblInner
GROUP BY
tblInner.P_id
) AS tblMax
ON tblMax.maxTime = node_weather.time
AND tblMax.P_id = node_weather.P_id
这将获得max time
和P_id
的最新组合。
发布于 2014-07-03 07:22:24
尝试这个查询。它比子查询快。
select nw1.* from node_weather nw1
LEFT JOIN node_weather nw2 on nw2.P_id = nw1.P_id and nw2.time>nw1.time
where nw2.W_ID is null;
发布于 2014-07-03 06:49:23
SELECT * FROM node_weather GROUP BY P_id ORDER BY time DESC
这应该能起作用
https://stackoverflow.com/questions/24546701
复制相似问题