简体   繁体   English

MYSQL和PHP组的结果由

[英]MYSQL and PHP group results by

I will like to display the following database in various html table group by Comp. 我想通过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

example table 1 示例表1

Comp: Africa World Cup - Qual
Kenya     Namibia
Zimbabwe  Mozambique

Example table 2 示例表2

England - League One
Coventry City Colchester Utd

etc. 等等

I tried 我试过了

SELECT GROUP_CONCAT(Comp) as Comp 
  FROM database 
 Group By Comp`

but no luck. 但没有运气。

Are you looking for this? 你在找这个吗?

SELECT comp,
       GROUP_CONCAT(CONCAT(TeamA, '|', TeamB)) details
  FROM table1
 GROUP BY comp

Output: 输出:

+--------------------------------------+--------------------------------------------+
| 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 |
+--------------------------------------+--------------------------------------------+

Here is SQLFiddle demo 这是SQLFiddle演示

If needed you can change delimiters both in CONCAT() and GROUP_CONCAT() from comma , and pipe | 如果需要,您可以更改分隔符既CONCAT()GROUP_CONCAT()从逗号,和管道| respectively. 分别。 You can easily explode() details values while iterating over the result set. 在迭代结果集时,您可以轻松地explode()详细信息值。

Simplified php part using PDO might look like this 使用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>';
}

Output: 输出:

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

I think you have a database design problem. 我认为你有数据库设计问题。 Do I understand that your output is to display games in a certain competition? 我是否理解您的输出是在某场比赛中展示游戏? So team A plays against team B in competition C? 那么A队在C队比赛中对阵B队?

You should create more than 2 tables I think. 你应该创建超过2个表。

  • Table A: containing all your teams. 表A:包含所有团队。
  • Table B containing all competitions (because probably a team could participate in more than one competition). 表B包含所有比赛(因为可能一个团队可以参加一个以上的比赛)。
  • Table C to join teams in competitions. 表C加入参赛队伍。
  • Table D to hold games in a certain competition. 表D在某个比赛中举行比赛。

In table D we only link records from table C. Reason is to prevent a team playing in a competition that it is not participating in. 在表D中,我们仅链接表C中的记录。原因是为了防止团队参加未参与的比赛。

So : 所以:

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)

Then the query isn't that hard: 然后查询不是那么难:

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

An example: Teams 一个例子:团队

1   Zimbabwe
2   Kenya
3   AJAX
4   Chelsea
5   Feyenoord
6   PEC Zwolle

Competitions 比赛

1   World Championship 2014
2   Dutch Premier League

TeamCompetitions 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

Games 游戏

1   2
2   3
7   6
7   8

Output of the query: 输出查询:

teamA   teamB   competition
Zimbabwe    Zimbabwe    World Championship 2014
Zimbabwe    Kenya   World Championship 2014
Feyenoord   AJAX    Dutch Premier League
Feyenoord   PEC Zwolle  Dutch Premier League

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM