Reputation: 420
for a soccer league i have two tables:
This is how my query looks like:
SELECT Teams, Sum(P) as 'Matches', Sum(W) as 'win', Sum(D) as 'draw', Sum(L) as 'lost',
SUM(Pts) as 'points'
FROM (
SELECT home Teams, 1 P,
IF (home > away,1,0) W,
IF (home = away,1,0) D,
IF (home < away,1,0) L,
CASE
WHEN home > away THEN 3
WHEN home = away THEN 1
ELSE 0
END PTS
FROM `matches`
UNION ALL
SELECT away Teams, 1,
IF (home < away,1,0),
IF (home = away,1,0),
IF (home > away,1,0),
CASE
WHEN home < away THEN 3
WHEN home = away THEN 1
ELSE 0
END
FROM `matches`
) AS ERG
GROUP BY Teams
ORDER BY SUM(Pts) DESC
Now i want the team names (teams.team_name) from the team-table. To achieve this i tried several join-statements with no luck.
It's obvious that the teams-table can contain teams who did not attend a match. These teams need to be displayed with zero-results.
Therefore I tried a LEFT JOIN:
SELECT team_name AS 'Teams'
FROM `teams`
LEFT JOIN matches ON ( teams.id = matches.home_team_id )
right after the ORDER-line in the end. I got an error message. I use MySQL 5.1.44, so nested selects shouldn't be a problem.
Any idea?
Upvotes: 0
Views: 3002
Reputation: 247680
It sounds like you want to do something like this. I updated your sub-queries to include the home_team_id
and the away_team_id
, then you will JOIN
on the teams
table to return the name.:
SELECT Teams,
Sum(P) as 'Matches',
Sum(W) as 'win',
Sum(D) as 'draw',
Sum(L) as 'lost',
SUM(Pts) as 'points',
h.team_name as HomeTeam,
a.team_name as AwayTeam
FROM
(
SELECT home Teams,
1 P,
IF (home > away,1,0) W,
IF (home = away,1,0) D,
IF (home < away,1,0) L,
CASE WHEN home > away THEN 3 WHEN home = away THEN 1 ELSE 0 END PTS,
home_team_id,
away_team_id
FROM `matches`
UNION ALL
SELECT away Teams,
1,
IF (home < away,1,0),
IF (home = away,1,0),
IF (home > away,1,0),
CASE WHEN home < away THEN 3 WHEN home = away THEN 1 ELSE 0 END,
home_team_id,
away_team_id
FROM `matches`
) AS ERG
LEFT JOIN `teams` h
on ERG.home_team_id = h.home_team_id
LEFT JOIN `teams` a
on ERG.away_team_id = a.away_team_id
GROUP BY Teams, home_team_id, away_team_id
ORDER BY SUM(Pts) DESC
Edit #1 based on your comments, it sounds like you want this (See SQL Fiddle with Demo):
SELECT TeamId,
t.team_name,
Teams,
Sum(P) as 'Matches',
Sum(W) as 'win',
Sum(D) as 'draw',
Sum(L) as 'lost',
SUM(Pts) as 'points'
FROM
(
SELECT home_team_id TeamId,
home Teams, 1 P,
IF (home > away,1,0) W,
IF (home = away,1,0) D,
IF (home < away,1,0) L,
CASE
WHEN home > away THEN 3
WHEN home = away THEN 1
ELSE 0
END PTS
FROM `matches`
UNION ALL
SELECT away_team_id TeamId,
away Teams, 1,
IF (home < away,1,0),
IF (home = away,1,0),
IF (home > away,1,0),
CASE
WHEN home < away THEN 3
WHEN home = away THEN 1
ELSE 0
END
FROM `matches`
) AS ERG
LEFT JOIN `teams` t
ON ERG.TeamId = t.id
GROUP BY Teams, TeamId
ORDER BY SUM(Pts) DESC
Upvotes: 1
Reputation: 33512
You can use an outer join like this:
SELECT a.team_name, Sum(ERG.P) as 'Matches', Sum(ERG.W) as 'win', Sum(ERG.D) as 'draw', Sum(ERG.L) as 'lost',
SUM(Pts) as 'points'
FROM
Teams a
left outer join
(
SELECT home Teams, 1 P,
IF (home > away,1,0) W,
IF (home = away,1,0) D,
IF (home < away,1,0) L,
CASE
WHEN home > away THEN 3
WHEN home = away THEN 1
ELSE 0
END PTS
FROM `matches`
UNION ALL
SELECT away Teams, 1,
IF (home < away,1,0),
IF (home = away,1,0),
IF (home > away,1,0),
CASE
WHEN home < away THEN 3
WHEN home = away THEN 1
ELSE 0
END
FROM `matches`
) AS ERG
on a.team_name=ERG.Teams
GROUP BY a.team_name
ORDER BY SUM(Pts) DESC
which should do the trick nicely for you.
I also wrote a rather lengthy question and answer which explains unions, joins, outer joins and all sorts of other goodies - which I link to when answering questions like this. It is very long and detailed and explains all the steps in detail as it goes.
Upvotes: 1