"Jonathan" <jo******@heelal.nl> wrote in message
news:d4**********@reader13.wxs.nl...
"Jonathan" <jo******@heelal.nl> wrote in message
I know my where clause is a bit complex but unfortunately (IMHO)
neccesary to find the teams based on the league/competition they are
playing at this year.
All teams play an indoor ( id_season=2 ) and and outdoor competition.
The outdoor season is split into two seasons because of the bad weather
in our country ( The weather can be harsh, during this time the indoor
competition is played ).
Certain teams play against the same teams before and after the indoor
competition ( id_season=4), other teams play two separted outdoor
competitions with different teams in a group. The first season (
id_season=1 ) starts before the indoor competition and the second one
starts after the indoor competition ends ( id_season = 3 ). The last
group of leages plays against the same teams before and after the indoor
competition ( therefore there are more teams in those leagues ) and for
them id_season = 4.
I can give you a simplofoed query by evaluating the query for today
(because I don't have a table which group consists of which teams when)
I filter the database based on the id_season code as a team is either
playing the second half of their league (4) or playing the second league
for this group this year (3):
SELECT DISTINCT
team
FROM
program
WHERE
program.group = 'D2B'
AND
(
id_season= 4
OR
id_season= 3
)
Slightly cleaner/leaner:
AND (id_season IN (3, 4))
Can you now (with the evaluated more simple query) help me join the two
queries together to find all the possible matches?
I was thinking somewhere along the line of this:
SELECT DISTINCT
teamsA.home as hometeam, teamsB.home as awayteam
FROM
program as teamsA, program As teamsB
WHERE
teamsA.group='D2B' AND teamsB.group='D2B'
AND
teamsA.home != teamsB.away
AND
(
(teamsA.id_season=3 or teamsA.id_season=4)
AND
(teamsB.id_season=3 or teamsB.id_season=4)
)
ORDER BY
hometeam, awayteam;
I'm hoping and guessing that there is some other and better way to do
this as I need to specify the criteria for both tables and have to use
the DISTINCT keyword as well to reduce the redundant records (7 fold
when the group consists of 8 teams).
Okay. I see now that you are drawing your team names from a table where any
single team may appear in more than one record. It seems you should have a
simple list of teams somewhere. No matter. You can get it on the fly.
CREATE TEMPORARY TABLE TeamList
SELECT DISTINCT home As TeamName
FROM program
WHERE (home.group = 'D2B')
AND (id_season IN (3,4));
Now you have a perfectly good table called "TeamList" with the names of all
teams in group 'D2B' with id_season = 3 or id_season = 4. The bulk of your
filtering is done.
SELECT HomeTeams.TeamName as HomeTeam,
AwayTeams.TeamName as AwayTeam
FROM TeamList As HomeTeams, TeamList As AwayTeams
ORDER BY HomeTeam, AwayTeam;
DROP TABLE TeamList;
That last DROP TABLE is not strictly necessary. Temporary tables are just
that - temporary. As soon as you close your connection, they disappear.
However, it is always good practice to deliberately DROP what you CREATE if
you don't want to keep something hanging around, even if the system is
friendly enough to do some of your housecleaning for you.
Or is creating temporary tables the only solution?
That's doubtful. But -
I think it is the *cleanest* solution to compensate for the lack of nested
queries.
And
It is a great way to simplify some of the excruciatingly complicated queries
we all seem to blunder about with because you can use temporary tables to
bust them up into sequences of 2 or more simpler queries.
And if so: If I use
temporary tables are they deleted after closing the connection or do I
have to take care of that in my code?
Temporary tables disappear when you close your connection. Nevertheless,
you might find it useful (and professional!) to deliberately DROP the temp
table when you are done. If you try to re-execute your query inside the
same connection, you will find that the original table you created standing
in the way when you try to CREATE it again. By ending your code with a DROP
TABLE, you eliminate this problem.
One of the biggest learning hurdles that seems to keep people limited in
their sql coding abilities is the failure to view their sql code as a
*chain* of table transformations. TableA -> TableB -> TableC. Viewing it
in this way opens a lot of possibilities that many seem to miss. People
(myself included!) try to cram everything into a single, impossible to
decipher, statement. I blame MySQLs lack of stored procedures for this
situation. However, temporary tables provide an excellent remedy once you
discover how to use them.
Thomas Bartkus