If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > multiple table query returning two different values based on same field name

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-06-04, 07:10
mabz mabz is offline
Registered User
 
Join Date: Apr 2004
Location: Wellington, NZ
Posts: 5
Question multiple table query returning two different values based on same field name

Hi,
Have struck a dead end on a result I want to do with only one query.

Basically there are two tables Fixture and Team.

Fixture has results of a sporting fixture where the two teams are represented by:
fixture_hometeamid and fixture_awayteamid and the primary key of fixture_gameid

Team has the details of sports teams and the primary key of
team_teamid with the field name team_name that I am trying to display in the result of the query twice basaed on the hometeamid and awayteamid.

Here is the query and the result. Sorry for the crap explanation but I don't really know how to explain it.

SELECT sport_fixture.fixture_hometeamid,sport_fixture.fix ture_awayteamid, sport_fixture.fixture_gameid, sport_team.team_name as Home, sport_team.team_name as Away
FROM
sport_fixture
LEFT OUTER JOIN sport_team ON sport_fixture.fixture_hometeamid = sport_team.team_teamid OR sport_fixture.fixture_awayteamid = sport_team.team_teamid
WHERE
sport_fixture.fixture_gameid = 1;

+--------------------+--------------------+----------------+-------------+-------------+
| fixture_hometeamid | fixture_awayteamid | fixture_gameid | Home | Away |
+--------------------+--------------------+----------------+-------------+-------------+
| 3 | 8 | 1 | Highlanders | Highlanders |
| 3 | 8 | 1 | Reds | Reds |
+--------------------+--------------------+----------------+-------------+-------------+
2 rows in set (0.00 sec)

The result I want is one row where Hoghlanders appear as Home and the Reds as away

Thanks for any help that can be offered
Reply With Quote
  #2 (permalink)  
Old 04-06-04, 07:19
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
PHP Code:
select sport_fixture.fixture_gameid
     
sport_fixture.fixture_hometeamid
     
home.team_name  as hometeam
     
sport_fixture.fixture_awayteamid
     
away.team_name  as awayteam
  from sport_fixture
left outer 
  join sport_team 
as home
    on sport_fixture
.fixture_hometeamid 
     
home.team_teamid 
left outer 
  join sport_team 
as away
    on sport_fixture
.fixture_awayteamid 
     
away.team_teamid
 where sport_fixture
.fixture_gameid 
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 04-06-04, 07:45
mabz mabz is offline
Registered User
 
Join Date: Apr 2004
Location: Wellington, NZ
Posts: 5
Cool

Brilliant, Cheers.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On