[英]inner join on 2 values (datetime and id) same table
I have 2 tables:我有2张桌子:
both tables have data specific to a device_id and datetime, for example device color:两个表都有特定于 device_id 和 datetime 的数据,例如设备颜色:
device_id, datetime, color
device_alerts:设备警报:
device_id, datetime, alert
now there can be multiple records per device per day.现在每个设备每天可以有多个记录。 So there can be multiple alerts and multiple colors for 1 device per day.
因此,每天 1 台设备可以有多个警报和多种颜色。 And there are multiple devices.
并且有多个设备。
I want to merge these 2 tables with a query.我想用查询合并这两个表。 But when I do this, I get way to many rows.
但是当我这样做时,我得到了很多行。 I don't know what I'm doing wrong.
我不知道我做错了什么。 This is my query (it's sql lite because it is in redash)
这是我的查询(它是 sql lite 因为它在 redash 中)
select a.alarm_type, a.created_at, a.monitor_id, dc.datetime, dc.device_id, dc.color
from query_505127 as a
inner join query_505241 as dc on a.monitor_id = dc.device_id
inner join query_505241 as dc1 on dc.datetime = a.created_at_date
Without example data it's hard to know exactly what you want..没有示例数据,很难确切地知道您想要什么..
This version joins to query_505241 once, using both fields as JOIN criteria which I think is what you are after.此版本加入 query_505241 一次,使用两个字段作为 JOIN 标准,我认为这就是您所追求的。
select a.alarm_type, a.created_at, a.monitor_id, dc.datetime, dc.device_id, dc.color
from query_505127 as a
inner join query_505241 as dc on a.monitor_id = dc.device_id
AND dc.datetime = a.created_at_date
This version is the same as your query but with the second JOIN being based on dc1.datetime rather than dc.datetime此版本与您的查询相同,但第二个 JOIN 基于 dc1.datetime 而不是 dc.datetime
select a.alarm_type, a.created_at, a.monitor_id, dc.datetime, dc.device_id, dc.color
from query_505127 as a
inner join query_505241 as dc on a.monitor_id = dc.device_id
inner join query_505241 as dc1 on dc1.datetime = a.created_at_date
Do either of these return what you'd expect?这些中的任何一个都会返回您所期望的吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.