我想通过Comp。
在各种html表组中显示以下数据库TeamA TeamB Comp Kenya Namibia Africa World Cup - Qual Zimbabwe Mozambique Africa World Cup - Qual Coventry City Colchester Utd England - League One Bray Wanderers UCD League Of Ireland - Premier Division Dundalk Drogheda United League Of Ireland - Premier Division
示例表1
Comp: Africa World Cup - Qual Kenya Namibia Zimbabwe Mozambique
示例表2
England - League One Coventry City Colchester Utd
等
我试过
SELECT GROUP_CONCAT(Comp) as Comp
FROM database
Group By Comp`
但没有运气。
答案 0 :(得分:2)
你在找这个吗?
SELECT comp,
GROUP_CONCAT(CONCAT(TeamA, '|', TeamB)) details
FROM table1
GROUP BY comp
输出:
+--------------------------------------+--------------------------------------------+ | comp | details | +--------------------------------------+--------------------------------------------+ | Africa World Cup - Qual | Kenya|Namibia,Zimbabwe|Mozambique | | England - League One | Coventry City|Colchester Utd | | League Of Ireland - Premier Division | Bray Wanderers|UCD,Dundalk|Drogheda United | +--------------------------------------+--------------------------------------------+
这是 SQLFiddle 演示
如果需要,您可以分别从逗号CONCAT()
和管道GROUP_CONCAT()
更改,
和|
中的分隔符。
迭代结果集时,您可以轻松explode()
详细说明值。
使用PDO的简化php部分可能看起来像这样
$db = new PDO('mysql:host=localhost;dbname=test', 'user', 'userpwd');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$sql = "SELECT comp,
GROUP_CONCAT(CONCAT(TeamA, '|', TeamB)) details
FROM table1
GROUP BY comp";
$query = $db->prepare($sql);
$query->execute();
$rows = $query->fetchall(PDO::FETCH_ASSOC);
$query = null;
$db = null;
foreach($rows as $row) {
echo $row['comp'] . '</br>';
$details = explode(',', $row['details']);
foreach($details as $detail) {
list($teama, $teamb) = explode('|', $detail);
echo $teama . ' - ' .$teamb . '</br>';
}
echo '</br>';
}
输出:
Africa World Cup - Qual Kenya - Namibia Zimbabwe - Mozambique England - League One Coventry City - Colchester Utd League Of Ireland - Premier Division Bray Wanderers - UCD Dundalk - Drogheda United
答案 1 :(得分:0)
我认为你有数据库设计问题。我是否理解您的输出是在某场比赛中展示游戏?所以A队在C队比赛中对阵B队?
我认为你应该创建超过2个表。
在表D中,我们仅链接表C中的记录。原因是为了防止团队参加未参与的比赛。
所以:
Table A (Teams) contains columns (idTeam INT, Teamname VARCHAR 50)
Table B (Competitions) contains columns (idComp INT, Competitionname VARCHAR 50)
Table C (TeamCompetitions) contains columns (idTeam INT, idComp INT)
Table D (Games) contains columns (idTeamCompA INT, idTeamCompB INT, idComp INT)
然后查询就不那么难了:
SELECT
TeamA.Teamname as teamA,
TeamB.Teamname as teamB,
comp.Competitionname as competition
FROM
Games
JOIN TeamCompetitions AS compTeamA ON compTeamA.idTeamCompetition = Games.idTeamCompA
JOIN Teams AS TeamA ON compTeamA.idTeam = TeamA.idTeam
JOIN TeamCompetitions AS compTeamB ON compTeamB.idTeamCompetition = Games.idTeamCompB
JOIN Teams AS TeamB ON compTeamB.idTeam = TeamB.idTeam
JOIN Competitions AS comp ON compTeamA.idCompetition = comp.idCompetition
一个例子: 队
1 Zimbabwe
2 Kenya
3 AJAX
4 Chelsea
5 Feyenoord
6 PEC Zwolle
竞赛
1 World Championship 2014
2 Dutch Premier League
TeamCompetitions
1 1 1
2 1 1
3 2 1
4 3 1
5 4 1
6 3 2
7 5 2
8 6 2
游戏
1 2
2 3
7 6
7 8
查询输出:
teamA teamB competition
Zimbabwe Zimbabwe World Championship 2014
Zimbabwe Kenya World Championship 2014
Feyenoord AJAX Dutch Premier League
Feyenoord PEC Zwolle Dutch Premier League
希望这有帮助!