我有足球(足球)统计数据库。我希望以这种方式展示前三支球队http://footballfacts.ru/turnircats/105166-chempionat-sssr
我在季节中有统计数据表,其中包含了这个名为clubseason的列(以及其他一些在这种情况下不感兴趣的内容):
我拥有的下一张桌子适用于俱乐部和季节,每个都有ids(idClub和idSeason)和名字(俱乐部名称和季节)。关系数据库中有三个表。有没有帮助以这种方式在PHP中输出SQL查询?
我的查询是这样的:
SELECT `jos_igraciDB_club`.`name` , `jos_igraciDB_season`.`idSeason`, `jos_igraciDB_season`.`name`,`jos_clubseason`.`clubPosition`, `jos_igraciDB_club`.`idClub`
FROM `jos_clubseason`
LEFT JOIN `jos_igraciDB_club` ON `jos_clubseason`.`idClub` = `jos_igraciDB_club`.`idClub`
LEFT JOIN `jos_igraciDB_season` ON `jos_clubseason`.`idSeason` = `jos_igraciDB_season`.`idSeason`
ORDER BY `jos_clubseason`.`clubPosition` ASC
答案 0 :(得分:1)
我不知道您拥有的确切数据库结构,因此您必须为名称等加入其他表,但这应该足以让您了解如何解决它。 http://sqlfiddle.com/#!9/d22e29/6
SELECT
cs.season,
first.club,
second.club,
third.club
FROM
clubseason cs
INNER JOIN clubseason first ON first.season = cs.season AND first.position = 1
INNER JOIN clubseason second ON second.season = cs.season AND second.position = 2
INNER JOIN clubseason third ON third.season = cs.season AND third.position = 3
GROUP BY cs.season
答案 1 :(得分:0)
SELECT
jos_igraciDB_season.name,
cnf.name, cns.name, cnt.name,
cs.idSeason,
first.idClub,
second.idClub,
third.idClub
FROM
jos_clubseason cs
INNER JOIN jos_clubseason first ON first.idSeason = cs.idSeason AND first.clubPosition = 1
INNER JOIN jos_clubseason second ON second.idSeason = cs.idSeason AND second.clubPosition = 2
INNER JOIN jos_clubseason third ON third.idSeason = cs.idSeason AND third.clubPosition = 3
LEFT JOIN jos_igraciDB_club cnf ON first.idClub = cnf.idClub AND first.clubPosition = 1
LEFT JOIN jos_igraciDB_club cns ON second.idClub = cns.idClub AND second.clubPosition = 2
LEFT JOIN jos_igraciDB_club cnt ON third.idClub = cnt.idClub AND third.clubPosition = 3
LEFT JOIN jos_igraciDB_season ON cs.idSeason = jos_igraciDB_season.idSeason
GROUP BY cs.idSeason