[vox-tech] SQL question (mysql)

David Hummel vox-tech@lists.lugod.org
Thu, 1 Apr 2004 01:32:51 -0800


On Wed, Mar 31, 2004 at 11:05:24PM -0800, dylan wrote:
> 
> I am having a problem getting a query to work with MySQL 3.23.49
> 
> I have 3 tables, all which share a common key (user_id):
> 
> 1. volunteer_info (contains contact information) [key = user_id]
> 2. assignment_data (contains event assignment information) [key =
> user_id] fields include: user_id, event_id
> each record represents one event that any one volunteer is assigned to
> 3. score_board (contains event participation information) [key =
> user_id] fields include: user_id, event_id, participation_status
> each record represents whether or not a person attended an event
> 
> there is a 1:1 relationship between each table, after a few 'where'
> statements are applied.
> 
> As it stands right now i can access every person who was assigned to a
> given event (denoted by the variable $event_id) with the following
> query:
> 
> select volunteer_info.*
>        from assignment_data, volunteer_info
>        where volunteer_info.user_id = assignment_data.user_id
>        and assignment_data.event_id = $event_id

If I understand your table structure correctly, here's another version:

select volunteer_info.*
from assignment_data
inner join volunteer_info using (user_id)
where assignment_data.event_id = $event_id

> i am able to access the contact info and participation info with this
> query:
> 
> select volunteer_info.*, participation_status
>        from assignment_data, volunteer_info, score_board
>        where volunteer_info.user_id = assignment_data.user_id
>        and volunteer_info.user_id = score_board.user_id
>        and assignment_data.event_id = $event_id
>        and score_board.event_id = $event_id
> 
> The score_board table may or may not contain records that match the
> second query, which causes an empty set to be returned - or worse yet
> - only some of the records that i am interested in. I have
> experimented with a left join, but am not quite sure how to accomplish
> this.

Then left join score_board onto the previous query:

select volunteer_info.*
from assignment_data
inner join volunteer_info using (user_id)
left join score_board using (user_id,event_id)
where assignment_data.event_id = $event_id

This way you'll still get contact/assignment info where score_board data
doesn't exist.

-David