結合構文は、暗黙的構文と明示的構文の誤った組み合わせであり、キーワードがJOIN
欠落しており、代わりに句内の結合条件が重複しています。JOIN
WHERE
SELECT
events.*,
attendees.*
FROM
attendees
JOIN events ON event.id = attendees.id
WHERE
event.id = <event to find attendees for>
PHPでアクセスできなくなる列名が重複するため、PHPで使用することはお勧めできません。events.*, attendees.*
代わりに、明示してください。
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>