首页 > 解决方案 > 返回参加的用户列表,其中出席在一个表中,名称在另一个表中

问题描述

我正在为我的电影俱乐部建立一个网站,以管理 covid 下的 RSVP。

我有以下查询:

SELECT
    e.id, 
    e.title, 
    e.tagline, 
    e.poster_img, 
    e.youtube_link, 
    e.synpois, 
    e.description, 
    e.date AS 'when',
    e.imdb,
    e.cap,
    COUNT(r.user_id) AS users, 
    l.name,
    l.address,
    l.website 
FROM event e 
LEFT JOIN rsvp r ON e.id = r.event_id 
JOIN location l ON l.id = e.venue
WHERE e.date >= CURDATE() 
GROUP BY e.id
ORDER BY e.date ASC

这会从事件数据更新或等于现在的“ event”表 ( ) 中选择所有事件。e

我加入了 " rsvp" 表并获得了回复他们将参加活动的用户的数量。

我还加入了一个“ location”表,其中包含电影放映地点的详细信息。

这一切都很好,我在 PHP 中使用了一个 while 循环,并使用所有这些详细信息创建了我的事件列表页面。返回的数据如下所示:

47 | The VelociPastor (2018) | Man of the clawth. | https://shitmovie.group/wp-content/uploads/2021/05... | 7Nyb0GqAjKM | After losing his parents, a priest travels to Chin... | Please RSVP so we can track attendees... | 2021-10-23 14:30:00 | https://www.imdb.com/title/tt1843303/ | 20 | 16 | The Hive Bar | 93 Erskineville Rd, Erskineville NSW 2043 | https://thehivebar.com.au/
55 | Tammy and the T-Rex (1994) | He's The Coolest Pet In Town! | https://shitmovie.group/wp-content/uploads/2021/05... | s2TjMvBmKuo | An evil scientist implants the brain of Michael, a... | Please RSVP so we can track attendees... | 2021-10-26 18:30:00 | https://www.imdb.com/title/tt0111361/ | 20 | 14 | The Hive Bar | 93 Erskineville Rd, Erskineville NSW 2043 | https://thehivebar.com.au/
54 | Cretaceous Park | https://shitmovie.group/wp-content/uploads/2018/08... | Fifth attempt at getting this going, we'll be goin... | 2021-10-30 18:00:00 | 99 | 16 | Federal Park Picnic Shelter | 2-2A Chapman Rd, Annandale NSW 2038 | https://www.cityofsydney.nsw.gov.au/parks/federal-...
49 | Poultrygeist: Night of the Chicken Dead (2006) | Humans... the other white meat... | https://shitmovie.group/wp-content/uploads/2021/05... | uWGKWkpC6ng | Zombified chickens attempt to kill the fast-food w...  | Please RSVP so we can track attendees and if you'r... | 2021-11-27 14:30:00 | https://www.imdb.com/title/tt0462485/ | 20 | 13 | The Hive Bar | 93 Erskineville Rd, Erskineville NSW 2043 | https://thehivebar.com.au/

但是,我还想显示那些已经回复的人的姓名列表,而不仅仅是我目前的人数 - 这是否可能在一个查询中(因此很容易输入我的 while 循环以获取 html一代)?

RSVP 的表格布局只是:

id
user_id
event_id
waitlist

id”只是每个条目的自动增量整数,“ user_id”是用户的 ID,“ event_id”是事件的 ID。(“ waitlist”你可以忽略,一个布尔值,如果他们在上限被击中后回复)。event" " & " " 表的布局location几乎是查询中的所有内容,并且希望从列名中可以自我解释。

我需要加入一个名为“ member”的表以返回相应的“ name”,例如:

JOIN member m ON m.id = r.user_id

我只是不确定如何r.user_id在原始查询中将所有“”作为列表返回,只需将“ r.user_id”添加到我的选择查询中即可返回找到的第一个值,我认为这GROUP BY e.id是我的“”造成的。

我已经尝试寻找解决方案,但不清楚如何实际描述我的问题足够短,让谷歌返回有用的东西,大多数回复只是如何加入我喜欢的表(如上所述)但不是更复杂的方式我制作这个 SQL 查询是为了让 PHP 方面的事情变得更容易。

最初我有一个网站,所有事件都点击到另一个页面,所以这很容易返回一个事件,但现在我一直在研究我的 CSS 和 AJAX,并将所有内容都拉到一个页面,这我认为效果更好(用户点击次数减少,全部集中在一个地方,希望到我完成后对移动设备更友好)。

如果对上下文有帮助,这里是原始站点:events.shitmovie.group,这是我正在处理上述代码以获得功能性poopointoh.shitmovie.group的新站点。

标签: mysql

解决方案


该查询似乎涉及“爆炸-内爆”场景。首先它从所有的 中收集一个大的临时表JOINing,然后通过GROUP BY. 这可能可以通过以下方式加速:

删除这两行

LEFT JOIN rsvp r ON e.id = r.event_id 
GROUP BY e.id

并更换

    COUNT(r.user_id) AS users, 

经过

    ( SELECT COUNT(*) FROM rsvp WHERE e.id = event_id ) AS users,

考虑使用GROUP_CONCAT()for 将用户名组合成一个字符串。


推荐阅读