1

我正在尝试获取队列中的召唤师今天玩的游戏数量。我尝试了几个查询。我得到的最接近的是:

   SELECT 
        s.summoner,
        s.wins,
        COUNT(*) as playedToday
   FROM summoners s
   LEFT JOIN teams_players tp ON tp.summoner_id = s.summoner
   LEFT JOIN teams t ON t.id = tp.team_id
   LEFT JOIN matches b ON t.id = b.blue_id
   WHERE
        b.played_on = DATE(NOW())
        s.in_queue;

但这并没有给出每个用户的计数。它只是获得总计数。

没有一张表有任何召唤师的数据。我想要的是这样的:

    +-----------------+------+-------------+
    | summoner        | wins | gamesPlayed |
    +-----------------+------+-------------+
    | DotAliscious    |  353 |           0 |
    | Kraator         |  440 |           0 |
    | Nammon          |  667 |           0 |
    | VictorousSecret |  843 |           0 |
    | Canas           |  544 |           0 |
    | Sprognak        |  502 |           0 |
    | Ghostilocks     |  808 |           0 |
    | b0b d0e         |  224 |           0 |
    | Metroshica      |  339 |           0 |
    | RubenatorX      |  478 |           0 |
    +-----------------+------+-------------+

我想不出如何笼统地表达这个问题,这就是为什么我还没有找到解决方案的原因。


我的数据集。请注意,teams.idmatches.blue_id和匹配matches.purple_id

mysql> DESCRIBE summoners;
+--------------+-------------+------+-----+---------+----------------+
| Field        | Type        | Null | Key | Default | Extra          |
+--------------+-------------+------+-----+---------+----------------+
| id           | int(11)     | NO   | PRI | NULL    | auto_increment |
| icon         | int(11)     | NO   |     | 0       |                |
| summoner     | varchar(32) | NO   | UNI | NULL    |                |
| skype        | varchar(32) | NO   |     | NULL    |                |
| email        | varchar(32) | NO   |     | NULL    |                |
| in_queue     | tinyint(1)  | NO   |     | 0       |                |
| wins         | int(11)     | NO   |     | 0       |                |
| level        | int(11)     | NO   |     | 1       |                |
| lan_wins     | int(11)     | NO   |     | 0       |                |
| played_today | int(11)     | NO   |     | 0       |                |
+--------------+-------------+------+-----+---------+----------------+
10 rows in set (0.00 sec)

mysql> DESCRIBE matches;
+-----------+-------------+------+-----+---------------------+----------------+
| Field     | Type        | Null | Key | Default             | Extra          |
+-----------+-------------+------+-----+---------------------+----------------+
| id        | int(11)     | NO   | PRI | NULL                | auto_increment |
| blue_id   | int(11)     | NO   | MUL | NULL                |                |
| purple_id | int(11)     | NO   | MUL | NULL                |                |
| status    | varchar(32) | NO   |     | display             |                |
| played_on | datetime    | NO   |     | 2012-04-06 13:53:55 |                |
+-----------+-------------+------+-----+---------------------+----------------+
5 rows in set (0.00 sec)

mysql> DESCRIBE teams;
+---------+-------------+------+-----+------------+----------------+
| Field   | Type        | Null | Key | Default    | Extra          |
+---------+-------------+------+-----+------------+----------------+
| id      | int(11)     | NO   | PRI | NULL       | auto_increment |
| outcome | varchar(32) | NO   |     | incomplete |                |
+---------+-------------+------+-----+------------+----------------+
2 rows in set (0.00 sec)

mysql> DESCRIBE teams_players;
+-------------+---------+------+-----+---------+----------------+
| Field       | Type    | Null | Key | Default | Extra          |
+-------------+---------+------+-----+---------+----------------+
| id          | int(11) | NO   | PRI | NULL    | auto_increment |
| team_id     | int(11) | NO   | MUL | NULL    |                |
| summoner_id | int(11) | NO   | MUL | NULL    |                |
+-------------+---------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
4

2 回答 2

1

played_ondatetime并且您正在将其与date可能是您的问题进行比较。你应该试试:

WHERE
    DATE(b.played_on) = DATE(NOW())

还有你是什么意思s.in_queue?你忘了 AND 吗?你的意思是

 WHERE
    DATE(b.played_on) = DATE(NOW())
    AND
    s.in_queue;
于 2012-04-06T20:54:04.643 回答
1

您将要使用子选择来获取满足条件的记录数,如果条件为假,它将返回 0。类似的东西

(SELECT COUNT(*) FROM matches b WHERE b.blue_id = t.id AND b.played_on = DATE(NOW())) AS playedToday

并且您将要删除表和检查日期的LEFT JOIN子句。matchesWHERE

于 2012-04-06T20:54:47.477 回答