Square1;10989608 wrote:
simple solution... DISTINCT eventsID (the Events table auto increment) and that SEEMS to work.
Once again, you are (probably not) selecting the correct data or are joining unnecessary tables.
Let's say you have 3 tables, event(id), player(id, event_id), player_detail(id, player_id) and the first table contains (1), i.e. one event with id: 1, the second contains (1, 1), (2, 1) and the third contains (1, 1), (2, 1). Reading this out, you have one event with two players, and the first of those players has two player details.
Actually, I doubt you have this strcuture, but it's just to be used as an example to explain what happens when selecting data, with or without dsitinct and group by. Also note that I do not bother with names for events, players and teams. I keep it down to ids.
If you do
SELECT event.id AS event
FROM event
INNER JOIN player ON event.id = player.event_id
you will get
If you also select player.id
SELECT event.id AS event, player.id AS player
FROM event
INNER JOIN player ON event.id = player.event_id
you'd get
That is, no matter what data is selected, you will have 2 rows. However, if you add DISTINCT to the first query, you'd get only one row instead of 2. In the second case, you'd still get 2 rows, since 1,1 is not the same as 1,2. That is, each ROW is still DISTINCT from all other rows.
But, if you only want distinct event ids, why are you even joining them to the player table? Why not simply
SELECT id FROM event
Since I do not know what queries you are actually executing since you only show half of them, and since I can't guess at the queries purpose (I don't care at all about sports so I can't even begin to relate to what you might want to do), I cannot say for certain that you are doing something wrong, but my instinct tells me it's likely.
My hunch tells me you are
1. either joining in unnecessary tables, not selecting the appropriate fields and/or not using aggregate functions and a group by clause properly,
2. or do not have an appropriate schema
For example, in my example above, there'd be no purpose at all to join all three tables, since a player has only participated with one team in any particular event, even though the result for player 1 would be
event player detail
1 1 1
1 1 2
And in this case I'd say that the error is in the schema. The player should be linked to teams via a link table and the teams should be linked to events via a link table. The team_player table might contain (player_id, team_id, start_date, end_date), with a primary key of (player_id, team_id, start_date).
The event table should contain start and end dates for each event, which means that the link table team_event only contains (event_id, team_id), with the primary key containing both fields.
Now the schema is correct. If you want to list events between certain dates, you'd still not link anything to the events table. If you however want to show the same list of events along with their teams, you might
SELECT event.id, GROUP_CONCAT(team.id)
FROM event
INNER JOIN team_event ON event.id = team_event.event_id
INNER JOIN team ON team.id = team_event.team_id
WHERE event.start_date BETWEEN @date_one AND @date_two
GROUP BY event.id
Or the same as above, except that the list contains event, team
SELECT event.id, team.id
FROM event
INNER JOIN team_event ON event.id = team_event.event_id
INNER JOIN team ON team.id = team_event.team_id
WHERE event.start_date BETWEEN @date_one AND @date_two
But once again, if you don't want team info, just event info, then do not join in team data.