I have a table of country teams
id country group
1 Poland 1
2 Greece 1
3 England 2
4 France 2
5 Germany 3
6 Spain 3
I also a table of scores for each country
fromId score
1 100
3 50
2 90
4 60
What I would like to do is get back a table of scores for each country within a group, having supplied just a country name. For example if I supply "France" then I would want the following table back
country score
England 50
France 60
I have managed to self join the country table with
SELECT
`t1`.`fldCountry`,
`t1`.`fldID`
FROM tblteam t1, tblteam t2
WHERE
t2.fldTeam = t1.fldTeam
AND
t2.fldCountry = 'France'
but am now stuck how to joing this back to get the data!
Please could anyone help a little?
Here you go
Country Table:
CREATE TEMPORARY TABLE Country
(
id INT,
country VARCHAR(20),
grp INT
)
INSERT INTO Country
(
id,
country,
grp
)
SELECT 1,
'Poland',
1
UNION ALL
SELECT 2,
'Greece',
1
UNION ALL
SELECT 3,
'England',
2
UNION ALL
SELECT 4,
'France',
2
UNION ALL
SELECT 5,
'Germany',
3
UNION ALL
SELECT 6,
'Spain',
3
Score Table:
CREATE TEMPORARY TABLE Score ( fromid INT, score INT )
INSERT INTO Score
(
fromid,
score
)
SELECT 1,
100
UNION ALL
SELECT 3,
50
UNION ALL
SELECT 2,
90
UNION ALL
SELECT 4,
60
Query:
SELECT b.country,
IFNULL(s.score, 0) score
FROM Country a
INNER JOIN Country b
ON a.grp = b.grp
LEFT JOIN score s
ON s.fromid = b.id
WHERE a.country = 'France'
Result:
Country Score
England 50
France 60
SELECT
c.country,
s.score
FROM
countries c
INNER JOIN scores s ON c.id = s.fromId
WHERE
c.group = (SELECT group FROM countries WHERE country = 'France' LIMIT 1);
Something like this should work.
Select C2.Country,SUM(S.Score)
FROM Country AS C1
INNER JOIN Country AS C2
ON C1.Group=C2.Group
INNER JOIN Scores AS S
ON C2.id = S.FromID
WHERE C1.Country=@Country
GROUP BY C2.Country
Where @Country is the country you want to look up, in your case, France.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.