在子查询 where 子句中引用列
Referencing a column in a sub-query's where clause
我有一个 table 有多个会话事件的记录。每一行都是一个会话中的一个事件,一个会话可以有多个相同的事件。这些基本上是游戏环节,每个事件都是回合开始或回合结束。我的数据看起来像
Session_id | Event_type | Event_Time
1 | round_start | 12:01:00
1 | round_end | 12:02:00
1 | round_start| 12:05:00
1 | round_end | 12:7:00
2 | round_start | 14:11:00
2 | round_end | 14:12:00
3 | round_start| 15:09:00
3 | round_end | 15:13:00
我正在尝试查找平均回合持续时间。我尝试了以下 SQL
select
RS.session_id,
RS.Event_Time as StartTime,
RE.EndTime,
TIMESTAMPDIFF(MINUTE,RE.EndTime,RS.Event_Time) as duration
from amp_event_mamlaka as RS
left join
(
select session_id, min(event_time) as EndTimd from amp_event_mamlaka
where Event_Type = "Round End" and session_id = RS.session_id and event_time>RS.Event_Time
) RE
on RE.session_id = RS.session_id
问题是我无法在加入的 table 中引用 RS.session_id 和 RS.event_time。
我正在使用 MySQL。关于如何实现这一点有什么建议吗?
谢谢
与其将其保留在 subquery
的 where
子句中,不如将条件保留在 Join On
子句中。试试这个。
SELECT RS.session_id,
RS.Event_Time AS StartTime,
RE.EndTime,
Timestampdiff(MINUTE, RE.EndTime, RS.Event_Time) AS duration
FROM amp_event_mamlaka AS RS
LEFT JOIN (SELECT session_id,
Min(event_time) AS EndTimd
FROM amp_event_mamlaka
WHERE Event_Type = "Round End") RE
ON RE.session_id = RS.session_id
AND RE.event_time > RS.Event_Time
我建议您使用相关子查询来解决这个问题:
select RS.session_id, RS.Event_Time as StartTime,
(select smin(event_time)
from amp_event_mamlaka em
where em.session_id = RS.session_id and
em.Event_Type = 'Round End' and
em.event_time > RS.Event_Time
) as EndTime,
from amp_event_mamlaka RS;
您可以使用子查询进行时间戳差异:
select RS.*, TIMESTAMPDIFF(MINUTE, EndTime, Event_Time) as duration
from (select RS.session_id, RS.Event_Time as StartTime,
(select min(event_time)
from amp_event_mamlaka em
where em.session_id = RS.session_id and
em.Event_Type = 'Round End' and
em.event_time > RS.Event_Time
) as EndTime
from amp_event_mamlaka RS
) RS
与嵌套查询相反,子查询应该只有 return 一个值。您的要求是一个示例,您需要来自成对行的数据。子查询仅用于连接对,不提供数据。 Fiddle
select e1.SessionID, e1.EventType, e1.EventTime, e2.EventType, e2.EventTime, TimeStampDiff( minute, e1.EventTime, e2.EventTime ) Duration
from Events e1
join Events e2
on e2.SessionID = e1.SessionID
and e2.EventType = 'end'
and e2.EventTime =(
select Min( EventTime )
from Events
where SessionID = e1.SessionID
and EventType = 'end'
and EventTime > e1.EventTime )
where e1.EventType = 'start';
我有一个 table 有多个会话事件的记录。每一行都是一个会话中的一个事件,一个会话可以有多个相同的事件。这些基本上是游戏环节,每个事件都是回合开始或回合结束。我的数据看起来像
Session_id | Event_type | Event_Time
1 | round_start | 12:01:00
1 | round_end | 12:02:00
1 | round_start| 12:05:00
1 | round_end | 12:7:00
2 | round_start | 14:11:00
2 | round_end | 14:12:00
3 | round_start| 15:09:00
3 | round_end | 15:13:00
我正在尝试查找平均回合持续时间。我尝试了以下 SQL
select
RS.session_id,
RS.Event_Time as StartTime,
RE.EndTime,
TIMESTAMPDIFF(MINUTE,RE.EndTime,RS.Event_Time) as duration
from amp_event_mamlaka as RS
left join
(
select session_id, min(event_time) as EndTimd from amp_event_mamlaka
where Event_Type = "Round End" and session_id = RS.session_id and event_time>RS.Event_Time
) RE
on RE.session_id = RS.session_id
问题是我无法在加入的 table 中引用 RS.session_id 和 RS.event_time。
我正在使用 MySQL。关于如何实现这一点有什么建议吗?
谢谢
与其将其保留在 subquery
的 where
子句中,不如将条件保留在 Join On
子句中。试试这个。
SELECT RS.session_id,
RS.Event_Time AS StartTime,
RE.EndTime,
Timestampdiff(MINUTE, RE.EndTime, RS.Event_Time) AS duration
FROM amp_event_mamlaka AS RS
LEFT JOIN (SELECT session_id,
Min(event_time) AS EndTimd
FROM amp_event_mamlaka
WHERE Event_Type = "Round End") RE
ON RE.session_id = RS.session_id
AND RE.event_time > RS.Event_Time
我建议您使用相关子查询来解决这个问题:
select RS.session_id, RS.Event_Time as StartTime,
(select smin(event_time)
from amp_event_mamlaka em
where em.session_id = RS.session_id and
em.Event_Type = 'Round End' and
em.event_time > RS.Event_Time
) as EndTime,
from amp_event_mamlaka RS;
您可以使用子查询进行时间戳差异:
select RS.*, TIMESTAMPDIFF(MINUTE, EndTime, Event_Time) as duration
from (select RS.session_id, RS.Event_Time as StartTime,
(select min(event_time)
from amp_event_mamlaka em
where em.session_id = RS.session_id and
em.Event_Type = 'Round End' and
em.event_time > RS.Event_Time
) as EndTime
from amp_event_mamlaka RS
) RS
与嵌套查询相反,子查询应该只有 return 一个值。您的要求是一个示例,您需要来自成对行的数据。子查询仅用于连接对,不提供数据。 Fiddle
select e1.SessionID, e1.EventType, e1.EventTime, e2.EventType, e2.EventTime, TimeStampDiff( minute, e1.EventTime, e2.EventTime ) Duration
from Events e1
join Events e2
on e2.SessionID = e1.SessionID
and e2.EventType = 'end'
and e2.EventTime =(
select Min( EventTime )
from Events
where SessionID = e1.SessionID
and EventType = 'end'
and EventTime > e1.EventTime )
where e1.EventType = 'start';