Mysql 请给出一些实现以下数据集数据集的方法

Mysql 请给出一些实现以下数据集数据集的方法,mysql,Mysql,在我的表格中有5个不同的事件1,2,3,4,5。在“与会者”列中,有获得这些活动的与会者。在isThisuserHost列中有0和1标志。1表示主办活动的主持人,1表示获得活动的与会者。我需要显示其中主持人为2,与会者为4的活动。在此表中,我们可以看到事件1、2和4是主机为2、与会者为4的行。如何使用单个查询提取结果。请看图片。提前谢谢 尝试以下查询: select distinct eventid from mytable m1 where 2 in (select attendee

在我的表格中有5个不同的事件1,2,3,4,5。在“与会者”列中,有获得这些活动的与会者。在isThisuserHost列中有0和1标志。1表示主办活动的主持人,1表示获得活动的与会者。我需要显示其中主持人为2,与会者为4的活动。在此表中,我们可以看到事件1、2和4是主机为2、与会者为4的行。如何使用单个查询提取结果。请看图片。提前谢谢


尝试以下查询:

select distinct eventid
from mytable m1
where 2 in (select attendee
            from mytable m2
            where m2.eventid = m1.eventid
            and isTheuserHost = 1
           )
and   4 in (select attendee
            from mytable m3
            where m3.eventid = m1.eventid
            and isTheuserHost = 0
           )

您需要在此处进行自连接:

select h.eventId
from myTable h
join myTable a using(eventId)
where h.isTheuserHost = 1
  and a.isTheuserHost = 0
  and h.attendee = 2
  and a.attendee = 4

h
a
是同一个表的不同别名
h
代表hoster,
a
代表anttendee,并带有
isTheuserHost
列的相应条件。然后,您可以使用该表,就好像它是两个一样。

如果下面的任何答案已经解决/帮助您解决了问题,请将其标记为答案并/或对答案进行投票。