您的连接语法是隐式和显式JOIN 语法的错误组合,缺少JOIN 关键字,而是在WHERE 子句中重复连接条件。
SELECT
events.*,
attendees.*
FROM
attendees
JOIN events ON event.id = attendees.id
WHERE
event.id = <event to find attendees for>
请注意,不建议在 PHP 中使用 events.*, attendees.*,因为您将有重复的列名,PHP 无法访问这些名称。相反,要明确:
SELECT
/* Be explicit about the columns you select in a JOIN query */
events.id AS event_id,
events.name AS event_name,
events.someothercol,
attendees.id AS attendee_id,
attendees.name AS attendee_name
FROM
attendees
JOIN events ON event.id = attendees.id
WHERE
event.id = <event to find attendees for>
如果即使没有与会者,您仍想获取活动详细信息,请改用LEFT JOIN:
SELECT
/* Be explicit about the columns you select in a JOIN query */
events.id AS event_id,
events.name AS event_name,
events.someothercol,
attendees.id AS attendee_id,
attendees.name AS attendee_name
FROM
events
/* LEFT JOIN will return event details even when there are no attendees */
LEFT JOIN attendees ON event.id = attendees.id
WHERE
event.id = <event to find attendees for>