Question

I have an event software. There is a table for individuals who sign up to an event and a table for teams that sign up to an event. Both individuals and teams can sign up to the same event. I also have a shirt table. This table will relate a shirt to an individual or a shirt to a team and shirts for teams will have multiple rows because teams are more than 1 individual. Here is the structure.

Individuals
-id
-event_id
-member_id

Teams
-id
-event_id
-team_name

shirts
-id
-entity_id
-shirt_id
-is_team

At first I had a separate individual shirt table and a separate team shirt table and tried to union all them to no avail. So now I have a single shirt table with the entity_id being either the individual id or the team id and the is_team to designate which table.

I'm struggling with a join to somehow get all the shirts for individuals and teams based on the event_id. I would have to show all the individual shirts and all the team shirts.

I can paste the union statement for my other table structure but I have nothing for the join to show. I'm just looking for a step in the right direction.

Was it helpful?

Solution

This what you probably need

select s.id,
s.shirt_id,
case 
 when s.is_team = 0 then i.member_id 
 else
 t.team_name 
END
as entity
from shirts s
left join Individuals i on i.id = s.entity_id AND s.is_team = 0
left join Teams t on t.id = s.entity_id AND s.is_team = 1

http://www.sqlfiddle.com/#!2/cbb687/5

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top