遍历列并分配 0/1 MySQL
Loop through column and assign 0/1 MySQL
我有一个关于参加活动的消费者的 MySQL 数据库。数据库包含用户 table、事件 table 和签到 table,它们都已连接。但是数据库只包含有关用户参加某个事件的信息。
我想要一个循环遍历几个事件并检查用户是否去了其中一个事件并给出结果的查询:
Preffered result
我试过以下查询:
select attend.idperson, attend.idmeeting,
(select case when
attend.idmeeting = 2901 or
attend.idmeeting = 9044 or
attend.idmeeting = 9161 or
attend.idmeeting = 2626
then 1 else 0 end) as attended
from attend
join meetings on attend.idmeeting = meetings.idmeeting
join persons on attend.idperson = persons.id
where meetings.verified = 1;
但这会带来人们实际参加的所有会议,如果 select 情况下的其中一个会议参加了查询,则分配 1,其余分配 0。目标是使用逻辑回归预测出勤率,此解决方案使所有进一步的用户数据都存在偏差。
您可以建立所有人员和会议的列表,然后加入出席详情:
select distinct a1.*,
case
when attend.idperson is null then 0
else 1
end as attended
from
(
select meetings.idmeeting, persons.id as p_id
from meetings
cross join persons
where meetings.verified = 1
) a1
left join attend
on attend.idmeeting = a1.idmeetings
and attend.personid a1.p_id
我有一个关于参加活动的消费者的 MySQL 数据库。数据库包含用户 table、事件 table 和签到 table,它们都已连接。但是数据库只包含有关用户参加某个事件的信息。 我想要一个循环遍历几个事件并检查用户是否去了其中一个事件并给出结果的查询:
Preffered result
我试过以下查询:
select attend.idperson, attend.idmeeting,
(select case when
attend.idmeeting = 2901 or
attend.idmeeting = 9044 or
attend.idmeeting = 9161 or
attend.idmeeting = 2626
then 1 else 0 end) as attended
from attend
join meetings on attend.idmeeting = meetings.idmeeting
join persons on attend.idperson = persons.id
where meetings.verified = 1;
但这会带来人们实际参加的所有会议,如果 select 情况下的其中一个会议参加了查询,则分配 1,其余分配 0。目标是使用逻辑回归预测出勤率,此解决方案使所有进一步的用户数据都存在偏差。
您可以建立所有人员和会议的列表,然后加入出席详情:
select distinct a1.*,
case
when attend.idperson is null then 0
else 1
end as attended
from
(
select meetings.idmeeting, persons.id as p_id
from meetings
cross join persons
where meetings.verified = 1
) a1
left join attend
on attend.idmeeting = a1.idmeetings
and attend.personid a1.p_id