从表A和B中选择,表C和表B中的记录不存在?(Select from table A and B, where records from table A and table B don't exist in table C?)
我无法查看我想要创建的查询的设置。
我有表A(用户)和表B(事件)。 对于每一个事件,我想要一个没有参加的用户列表。
我想第三个表是必要的,将用户与事件关联起来,同时包含用户和事件ID。 如果表中没有用户和事件ID组合,则用户没有参加。
我的查询如何查看,以便返回每个未参加每个活动的用户列表?
或者,我可以查询事件表,循环查看结果,并且对于每个事件,我可以查询第三个表中没有匹配事件ID的记录的用户。 这看起来效率很低,显然是错误的,因为有很多额外的db调用。
foreach ($getEvents as $v) { SELECT userID FROM users WHERE userID NOT IN (SELECT userID FROM tableC WHERE eventID = '".$v["eventID"]."'); }
如有需要,欢迎提供更多信息。
I'm having trouble visualising the setup for a query I'd like to create.
I have table A (users) and table B (events). For every event, I would like a list of users who did NOT attend.
I imagine a third table is necessary, associating the users with the events, containing both the user and event ID. If a user and event ID combination is absent from the table, the user did not attend.
How would my query need to look in order to return a list of every user who did not attend each event?
Alternatively I could query the events table, loops through the results, and for each event I could query the users who do not have a record with matching event ID in the third table. This seems inefficient and obviously wrong, as many additional db calls are made.
foreach ($getEvents as $v) { SELECT userID FROM users WHERE userID NOT IN (SELECT userID FROM tableC WHERE eventID = '".$v["eventID"]."'); }
Happy to provide more information if necessary.
原文:https://stackoverflow.com/questions/48179116