MySQL - 2个外键

时间:2016-03-01 15:49:01

标签: mysql count foreign-keys

我正在为棒球队做一个联赛桌。假设我有以下2个DB表:

小组

ID  | NAME
---------------------------
1   | Phillies
2   | Yankees
3   | Red Sox

游戏

ID | TEAM1_ID | TEAM2_ID | TEAM1_SCORE | TEAM2_SCORE
----------------------------------------------------
1  | 2        | 1        | 12          |  8
2  | 3        | 2        |  9          | 14
3  | 1        | 2        | 10          |  5

我希望MySQL能够列出所有团队及其结果。这包括总比赛,赢球和输球。

有谁知道如何做到这一点?

例如,结果集可能如下所示:

team_name | total_played | total_won | total_lost
-------------------------------------------------
Phillies  | 2            | 1         | 1
Yankees   | 3            | 2         | 1
Red Sox   | 1            | 0         | 1

谢谢!

4 个答案:

答案 0 :(得分:2)

这是使用conditional aggregation的一个选项:

select t.name, 
  count(g.id) total_played,
  sum(case when (t.id = team1_id and team1_score > team2_score) then 1
           when (t.id = team2_id and team2_score > team1_score) then 1 
           else 0
      end) total_won,
  sum(case when (t.id = team1_id and team1_score < team2_score) then 1
           when (t.id = team2_id and team2_score < team1_score) then 1 
           else 0
      end) total_lost
 from teams t
   left join games g on t.id in (g.team1_id, g.team2_id)
 group by t.name

答案 1 :(得分:1)

您可以这样做:

int guesssum; //declare this
.
.
if (int.TryParse(txtUserAnswer.Text, out guesssum)) //use guesssum here
{
    if (guesssum == sum){
        MessageBox.Show("Correct Answer!", "Correct");
        correct++;
    }
    else {
         //wrong, do something!
    }
}

这基本上可以让游戏桌更舒适地使用,使每个团队的记录倍增,因此计数很容易,并且不必检查多个条件。

答案 2 :(得分:1)

这可能与sgeddes解决方案相同。
我把条件放在子查询中,所以我可以先测试内部结果

<强> SQL Fiddle Demo

SELECT t.`NAME`, 
       SUM(play) as total, 
       SUM(WON) as games_won, 
       SUM(LOST) as games_lost,
       SUM(SCORE) as total_score
FROM (
      SELECT t.`NAME`,
             IF(g.`TEAM1_ID`IS NULL, 0, 1) as play,
             CASE WHEN t.`ID` = g.`TEAM1_ID` AND TEAM1_SCORE > TEAM2_SCORE THEN 1
                  WHEN t.`ID` = g.`TEAM2_ID` AND TEAM1_SCORE < TEAM2_SCORE THEN 1 
                  ELSE 0
             END as WON,
             CASE WHEN t.`ID` = g.`TEAM1_ID` AND TEAM1_SCORE < TEAM2_SCORE THEN 1
                  WHEN t.`ID` = g.`TEAM2_ID` AND TEAM1_SCORE > TEAM2_SCORE THEN 1 
                  ELSE 0
             END as LOST,
             CASE WHEN t.`ID` = g.`TEAM1_ID` THEN TEAM1_SCORE
                  WHEN t.`ID` = g.`TEAM2_ID` THEN TEAM2_SCORE
                  ELSE 0
             END as SCORE                         
      FROM teams t
      LEFT JOIN games g
        ON t.`ID` = g.`TEAM1_ID`
        OR t.`ID` = g.`TEAM2_ID`
   ) T
GROUP BY t.`NAME`

<强>输出

|     NAME | total | games_won | games_lost | total_score |
|----------|-------|-----------|------------|-------------|
| Phillies |     2 |         1 |          1 |          18 |
|  Red Sox |     1 |         0 |          1 |           9 |
|     Some |     0 |         0 |          0 |           0 |
|  Yankees |     3 |         2 |          1 |          31 |

答案 3 :(得分:0)

这样的事情应该有效:

 select id, name,
 (select count(*) from games where team1_id = id OR team2_id = id) as total_played,
 (select count(*) from games where (team1_id = id and team1_score > team2_score) OR (team2_id = id and team1_score < team2_score)) as total_won,
 (select count(*) from games where (team1_id = id and team1_score < team2_score) OR (team2_id = id and team1_score > team2_score)) as total_lost
from teams

Here是SQL小提琴。

相关问题