[英]Select corresponding value from second table (Mysql)
Struggling with some sql, would appreciate some guidance. 与一些SQL斗争,将不胜感激一些指导。
Have two tables logs and sense 有两个表的日志和意义
logs – 日志–
assetid ts nodeid status
1 2017-10-26 14:00:10 73 240
2 2017-10-26 14:00:06 21 160
3 2017-10-26 14:00:04 18 230
4 2017-10-26 14:00:02 19 400
5 2017-10-26 14:00:00 21 190
1 2017-10-26 13:20:08 18 20
2 2017-10-26 13:06:10 20 160
3 2017-10-26 13:03:04 17 230
sense – 感–
status value
20 5
160 37
190 39
230 56
240 58
400 90
Trying to find the correct syntax to only show the latest record (in datetime) of each assetid and then show the corresponding value from the sense table (based on the matching status in both tables) to produce – 尝试找到正确的语法以仅显示每个资产ID的最新记录(日期时间),然后显示感知表中的相应值(基于两个表中的匹配状态)以产生–
assetid ts nodeid status value
1 2017-10-26 14:00:10 73 240 58
2 2017-10-26 14:00:06 21 160 37
3 2017-10-26 14:00:04 18 230 56
4 2017-10-26 14:00:02 19 400 90
5 2017-10-26 14:00:00 21 190 39
Have tried – 试过 -
Select assetid, ts, nodeid, status, value
From
logs
Join sense X on X.status = logs.status
Group by assetid
Order by ts DESC
But this only outputs 1 row (instead of 5) 但这只会输出1行(而不是5行)
assetid ts nodeid status value
1 2017-10-26 14:00:10 73 240 58
Removing 删除
Join sense X on X.status = logs.status
of course outputs all records but that is not required. 当然会输出所有记录,但这不是必需的。
Thoughts appreciated. 思想表示赞赏。
Regards 问候
Active 活性
Actually your query is returning 5 rows, 1 for each id. 实际上,您的查询返回5行,每个ID返回1行。 But it won't return rows with latest ts for each id. 但它不会为每个ID返回带有最新ts的行。 You can verify this by clicking on the link for demo. 您可以通过单击演示链接来验证这一点。 You can compare results of both queries. 您可以比较两个查询的结果。
To achieve this task,following query will help you: 要完成此任务,以下查询将帮助您:
Select l.assetid, l.ts, logs.nodeid, X.status, X.value
From
logs
inner Join sense X on X.status = logs.status
inner join (select assetid, max(ts) as ts from logs group by assetid) l
on l.assetid = logs.assetid and logs.ts = l.ts
Group by l.assetid
Order by l.ts DESC;
EDIT: 编辑:
If dataype of ts
is string then replace max(ts)
in above query with: 如果ts
dataype是字符串,则将上述查询中的max(ts)
替换为:
max(str_to_date(ts,'%d%m%y'))
Feel free to ask any doubts. 随时提出任何疑问。
Hope it helps! 希望能帮助到你!
Use GROUP BY
to find minimum for each assetid and then JOIN
with the logs
and sense
使用GROUP BY
查找每个资产ID的最小值,然后JOIN
logs
并进行sense
Select *
FROM logs l
JOIN sense s ON s.status = l.status
JOIN
(
Select assetid, max(ts) maxts
From logs
Group by assetid
) t ON t.assetid = l.assetid and l.ts = t.maxts
Try this 尝试这个
Select a1.assetid, MAX(a1.ts), a1.nodeid, a1.status, X.value
From
logs a1
inner join sense X on X.status = a1.status
Group by assetid, a1.nodeid, a1.status, X.value
Order by ts DESC
On MY SQL 8.0.2 在MY SQL 8.0.2上
WITH CTE as
(
Select A.assetid, A.ts, A.nodeid, A.status, B.value, row_number() over(PARTITION BY A.assetid ORDER BY A.ts DESC) AS rn
from logs as A
inner join sense B ON A.status=B.status
)
SELECT *
FROM CTE
WHERE rn='1';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.