This topic is locked
[SOLVED]

 INNER JOIN on many-to-many relationship

11/4/2006 9:39:35 PM
PHPRunner General questions
R
runey author

A bit of a hard one to describe so please bear with me...
I have a table structure that looks a bit like this...

_events _event_guests _guests

- event_id----------> - event_id

- event_name - guest_id--------> - guest_id

- etc etc - guest_name

- etc etc


So event_id is linked with guest_id through another table called _event_guests....

This allows me to attach multiple guests to multiple events...

All set up with a master / detail relationship...

This all works great....
What I need to do is pull the guest_id and / or guest_name from _guests table into _events table...

I need to do this so I can search for a specific guest and return all the events they are attending...
I have tried many variations on INNER JOIN but can't get one to work...
My existing SQL qeury looks like this...

SELECT

`_events`.`event_id`,

`event_name`,

`event_website`,

`event_location`,

`event_address`,

`event_postcode`,

`continent_id`,

`country_id`,

`state_id`,

`city_id`,

`event_start_date`,

`event_finish_date`,

`_events`.`organiser_id`,

`_organisers`.`organiser_name`,

`_organisers`.`organiser_website`,

`_organisers`.`organiser_logo`,

`_event_guests`.`guest_id`

FROM `_events`

INNER JOIN `_organisers` ON `_organisers`.`organiser_id`=`_events`.`organiser_id`

INNER JOIN `_event_guests` ON `_event_guests`.`event_id`=`_events`.`event_id`


As you can see I already have an INNER JOIN to get info from the _organisers table as well...

However it creates a couple of problems...

  1. The events list is now incorrect... It will not show an event if no guest is associated... Also if an event has multiple guests then it appears multiple times... I need the event list to show all events once regardless of whether they currectly have guests associated with them yet...
  2. When attempting to search for an event based on a guest name I get the following error... Unknown column '_events.guest_id' in 'where clause'
    Any help will be greatly appreciated
    EDIT: Changing the second INNER JOIN to a LEFT JOIN shows events that currently have no guests, but still shows events multiple times if that event has mulitple guests... One step closer <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=question&id=3758&image=1&table=forumtopics' class='bbc_emoticon' alt=':)' />
    EDIT: OK another step closer I hope... Added the following line which gets rid of my duplicate events showing up <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=question&id=3758&image=2&table=forumtopics' class='bbc_emoticon' alt=':D' />

GROUP by `_events`.`event_id`


So the search problem still remain <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=question&id=3758&image=3&table=forumtopics' class='bbc_emoticon' alt=':(' />
EDIT: OK I think I have it all sorted... Changed a line in commonfunctions.php...

if($table=="_events" && $field=="guest_id") return "`_events`.`guest_id`";


with this...

if($table=="_events" && $field=="guest_id") return "`_event_guests`.`guest_id`";


Haven't fully tests it yet, but it seems to work just fine...
Amazing how just asking the question forces you to re-think the problem in the first place <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=question&id=3758&image=4&table=forumtopics' class='bbc_emoticon' alt=':)' />

T
thesofa 11/5/2006

Hi

These things I have learnt, they may help

If there is no entry in one of the tables that are inner joined to each other, the corresponding entry in t'other table will not show.

The way around this is to use a LEFT JOIN instead of an inner join.

the way to show only single entries for the master table is to use the DISTINCT sommand in front of the ID fiels, so it would read

[/code]SELECT DISTINCT

`_events`.`event_id`,

`event_name`,

`event_website`, [code]

Having re-read your post, it seems you are sorted. Distinct might take less processing than a group command.

R
runey author 11/5/2006

Oddly using DISTINCT did not work <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=12534&image=1&table=forumreplies' class='bbc_emoticon' alt=':blink:' /> Which was why I ended up using GROUP...
Having come from using MS Access in the past all these JOINs were getting me all confused...
Still I'm getting there slowly <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=12534&image=2&table=forumreplies' class='bbc_emoticon' alt=':)' />
Thanks for having a look...