Ravens PHP Scripts: Forums
 

 

View next topic
View previous topic
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> MySQL
Author Message
Gremmie
Former Moderator in Good Standing



Joined: Apr 06, 2006
Posts: 2415
Location: Iowa, USA

PostPosted: Mon Dec 04, 2006 2:10 pm Reply with quote

This is over my head. You don't have to solve this for me, just perhaps give me a pointer or two to go looking.

I have a table that represents "matches" (date, time, home team id , away team id) and a table for teams (name, color, etc). The home team id and away team id in the matches table are indices into the teams table.

I was writing a block to display upcoming matches. I wanted to write one select statement that would return the date, time, home team name, home team color, away team name, away team color. But since my matches table has two colums that index into the teams table I was kind of stumped.

I solved it by breaking it down into two SQL queries. First I just did a select * on the teams table to get all that info. Then I ran my query on the matches table and used PHP code to combine the data. But I couldn't help but wonder if there was a way to do this with only 1 SQL query using some technique I'm not aware of.

If there was only 1 team ID in the matches table I could handle that (with a "WHERE matches.home_id = teams.team_id). But there are two and I couldn't get my mind wrapped around that.

Any ideas? Thanks!
 
View user's profile Send private message
fkelly
Former Moderator in Good Standing



Joined: Aug 30, 2005
Posts: 3312
Location: near Albany NY

PostPosted: Mon Dec 04, 2006 4:11 pm Reply with quote

There may be a SQL expert around who can figure out how to do this with a doublesecret_inner_outer_join or something and I'd be prepared to learn from them. But I don't see any immediate solution without a few SQL calls.

I can see where you'd really want two types of reports from this: a matches report and a teams report. The matches report would show what teams are in each match and the teams report would show, for a given team, what matches they have.

For the first, matches report, I think that what you'd do is read the matches (perhaps selected the date range you are interested in or at least sorting by date) table and stick it in a while loop. Then for each record in the loop you'd retrieve the home team id and away team id and look in the teams table for the color and name. You could retrieve both team records with on SQL call by doing an OR but then you'd have to test them in a while loop to see if you matched on the home team or away team id. So you might as well just do two SQL calls I think and just get the home team or away team id specifically. Stick them into variables and print a line in your rfeport when you have processed both records, then go onto the next match record.

For the teams report what you'd do is find all the team records and stuff them into a while loop. Then find the matches where a given team is either a home team or an away team. You'd have the name and color of the team you are processing at the time in the loop but you'd need to do a lookup on the other team. Stuff these all into variables and echo out one line for each match that you find.

I'll be interested to see if anyone comes up with better "magic" than this and thanks for the intriguing question.
 
View user's profile Send private message Visit poster's website
Gremmie







PostPosted: Mon Dec 04, 2006 5:20 pm Reply with quote

Well as I said, I solved it with 2 calls. I built a "teams" array from one SELECT team_id, team_name, team_color FROM teams. The $teams array was indexed by team_id. Each element in this array was an array itself containing name and color.

Then the second select got the upcoming matches...something like SELECT date, time, home_id, away_id FROM matches WHERE date >= CURDATE().

I looped over that result set, and used my $teams array that I had previously built whenever I needed team information...e.g. $teams[$row['home_id']]['color']

I'm like you though, there must be some double secret join that I don't know about that could give me all this in one SELECT call. Smile

I'm going to go read up on some SQL statements that I don't know anything about...like UNION for example, and see what they do..
 
fkelly







PostPosted: Mon Dec 04, 2006 6:12 pm Reply with quote

Well post the results back here. You are way ahead of me on this. When I was learning databases they didn't have SQL, and that sure dates me.
 
Gremmie







PostPosted: Mon Dec 04, 2006 6:58 pm Reply with quote

I tried playing with union, and it simply concatenates the rows from one select with the rows from another. So this sql:

Code:


SELECT m.match_date, m.match_time, m.match_home, m.match_away, t.team_name
FROM nuke_clan_matches AS m, nuke_clan_teams AS t
WHERE m.match_date >= CURDATE( )
AND m.match_home = t.team_id
UNION SELECT m.match_date, m.match_time, m.match_home, m.match_away, t.team_name
FROM nuke_clan_matches AS m, nuke_clan_teams AS t
WHERE m.match_date >= CURDATE( )
AND m.match_away = t.team_id


produced 2 rows for each match...one with the team_name home and the other the away team name.

That isn't quite what I wanted. I'll have to look into the crazy join syntax next.
 
montego
Site Admin



Joined: Aug 29, 2004
Posts: 9457
Location: Arizona

PostPosted: Tue Dec 05, 2006 6:42 am Reply with quote

Gremmie, question for you. Would my assumption always be 100% correct that your matches table ALWAYS has both home team id and away team id valued AND you have sufficient data integrity rules in place to ensure that these same id's will have "hits" in the teams table?

If this assumption is correct, you simply join twice to the teams table, using the home team id for one and away team id for the second. No fancy INNER/OUTER JOINS are necessary if the assumption is always true.

_________________
Where Do YOU Stand?
HTML Newsletter::ShortLinks::Mailer::Downloads and more... 
View user's profile Send private message Visit poster's website
Gremmie







PostPosted: Tue Dec 05, 2006 9:54 am Reply with quote

Well up until recently I thought so...but that is beside the point. Let's say that the assumption is still correct...how do I join twice to the teams table? I'm struggling with that syntax. I think there is something in here to help me:
[ Only registered users can see links on this board! Get registered or login! ]

but I haven't quite grokked it yet.
 
montego







PostPosted: Tue Dec 05, 2006 8:04 pm Reply with quote

It might look something like this:

Code:


SELECT m.match_date, m.match_time, m.match_home, m.match_away, t1.team_name AS team_name_home, t2.team_name AS team_name_away
FROM nuke_clan_matches AS m, nuke_clan_teams AS t1, nuke_clan_teams AS t2
WHERE m.match_date >= CURDATE( )
AND m.match_home = t1.team_id
AND m.match_away = t2.team_id


Then you could access the different names like this:

$row['team_name_away'] or $row['team_name_home']
 
Gremmie







PostPosted: Tue Dec 05, 2006 8:40 pm Reply with quote

Awesome...thanks! I will try this out.

Do you have a link to a good SQL tutorial?
 
Gremmie







PostPosted: Tue Dec 05, 2006 9:54 pm Reply with quote

Yup, worked great. Thanks for the tip. I did not know you could join from the same table twice. Makes sense now.
 
montego







PostPosted: Wed Dec 06, 2006 6:05 am Reply with quote

You are welcome. Regarding a good tutorial, unfortunately, I am not aware of one. But, I also have not looked. Maybe others here can recommend something.
 
Guardian2003
Site Admin



Joined: Aug 28, 2003
Posts: 6799
Location: Ha Noi, Viet Nam

PostPosted: Wed Dec 06, 2006 10:15 am Reply with quote

Here's an excellent one [ Only registered users can see links on this board! Get registered or login! ]
 
View user's profile Send private message Send e-mail
fkelly







PostPosted: Wed Dec 06, 2006 6:31 pm Reply with quote

ROFL, Guardian ... I haven't laughed that hard in years. A join on a post?
 
montego







PostPosted: Wed Dec 06, 2006 7:34 pm Reply with quote

OMG, was that an Embarassed or Brit humor coming out! That is just too good either way... ROTFL
 
Guardian2003







PostPosted: Thu Dec 07, 2006 3:44 am Reply with quote

Just my dry Brit humour Wink
 
Display posts from previous:       
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> MySQL

View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB © 2001-2007 phpBB Group
All times are GMT - 6 Hours
 
Forums ©