我有以下表格:
members:
----------------------------------------------
| member_id | member_name | member_join_date |
----------------------------------------------
events:
-------------------------
| event_id | event_name |
-------------------------
event_dates:
--------------------------------------
| event_id | event_date | event_week |
--------------------------------------
attendance:
------------------------------------------------------------
| member_id | event_id | attendance_date | attendance_week |
------------------------------------------------------------
所以我有成员在某个时间加入该组织,他们可能参加每周活动(其中发生的事件由event_dates
表监控)
我想做的是让所有至少连续缺席3次的成员
我尽我所能,我认为我有查询的每一部分,但不知道如何将它们合并在一起。
获取他们可以参加的所有活动:
SELECT
m.member_id AS id,
ed.event_id,
ed.event_week
FROM
members AS m
LEFT JOIN
event_dates AS ed
ON
ed.event_date >= m.member_join_date
获取他们实际参加的所有活动:
SELECT
m.member_id AS id,
a.event_id
FROM
members AS m
LEFT JOIN
attendance AS a
ON
a.member_id = m.member_id
如果我设法将两个查询合并为一个,那么可能是这个问题的答案:
Count of consecutive not null values
但这次我想计算NULL
值
答案 0 :(得分:0)
你可以尝试下面的内容:
select * from (select m.*,k.*,
@rn := CASE WHEN bevent_id is null THEN @rn+1 ELSE 0 END AS rn
from(
SELECT
m.member_id AS id,
ed.event_id ,event_date
FROM
members AS m
left JOIN
event_dates AS ed
ON
ed.event_date >= m.member_join_date) m
left join (SELECT
m.member_id AS bid,
a.event_id as bevent_id
FROM
members AS m
LEFT JOIN
attendance AS a
ON
a.member_id = m.member_id) k on m.id=k.bid
and m.event_id=k.bevent_id , (SELECT @rn := 0) AS vars
order by m.id,m.event_date ) main
where rn>=3
使用您提供的两个查询,我执行左连接以比较两个查询的结果。 为了跟踪成员是否缺席3连续事件,我使用@rn进行跟踪。