1

我有以下表格:

Table "Questoes";
+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| id       | int(11)      | NO   | PRI | NULL    | auto_increment |
| id_quest | int(11)      | NO   |     | NULL    |                |
| questao  | varchar(255) | NO   |     | NULL    |                |
| nivel    | int(11)      | NO   |     | NULL    |                |
| tipo     | varchar(255) | NO   |     | NULL    |                |
+----------+--------------+------+-----+---------+----------------+

Table "Resultados";

+-----------+---------+------+-----+---------+----------------+
| Field     | Type    | Null | Key | Default | Extra          |
+-----------+---------+------+-----+---------+----------------+
| id        | int(11) | NO   | PRI | NULL    | auto_increment |
| id_user   | int(11) | NO   | MUL | NULL    |                |
| nivel     | int(11) | NO   |     | NULL    |                |
| pontuacao | int(24) | NO   |     | NULL    |                |
| data      | date    | NO   |     | NULL    |                |
+-----------+---------+------+-----+---------+----------------+

And table "utilizador";
+-----------+--------------+------+-----+---------+----------------+
| Field     | Type         | Null | Key | Default | Extra          |
+-----------+--------------+------+-----+---------+----------------+
| id_user   | int(11)      | NO   | PRI | NULL    | auto_increment |
| id_tipo   | int(11)      | NO   |     | 1       |                |
| username  | varchar(50)  | NO   | UNI | NULL    |                |
| password  | varchar(20)  | NO   |     | NULL    |                |
| nome      | varchar(50)  | NO   |     | NULL    |                |
| email     | varchar(100) | NO   |     | NULL    |                |
| data_nasc | text         | NO   |     | NULL    |                |
| e_valido  | smallint(6)  | NO   |     | 0       |                |
+-----------+--------------+------+-----+---------+----------------+

我需要的是“resultados”表中每个级别(“nivel”字段)的最高得分(“pontuacao”字段)给最佳玩家。要获取球员姓名,将通过“resultados”表中的“id_user”字段。

我有这个,但不起作用:

SELECT u.id_user, MAX(u.pontuacao), u.nivel, u.data, l.nivel, r.id_user, r.username
FROM questoes l, resultados u, utilizador r
WHERE u.nivel = l.nivel AND r.id_tipo=1 AND u.id_user=r.id_user
GROUP BY u.nivel    
4

2 回答 2

1
SELECT a.id_user, r.username, b.pontuacao, b.nivel
FROM resultados a
INNER JOIN utilizador r on a.id_user=r.id_user 
INNER JOIN (SELECT u.nivel, MAX(u.pontuacao) pontuacao
            FROM resultados u
            GROUP BY u.nivel) b ON a.nivel = b.nivel AND a.pontuacao = b.pontuacao
于 2013-08-27T16:09:20.413 回答
1

它不起作用,因为您依赖的 MySQL 扩展没有达到您的预期。使用 时group by,只需确保 中的所有列select都是聚合的或group by.

你想要的更像是:

SELECT u.id_user, r2.pontuacao, u.nivel, u.data, l.nivel, r.id_user, r.username
FROM questoes l join
     resultados u
     u.nivel = l.nivel join
     utilizador r 
     on u.id_user = r.id_user join
     (select r2.nivel, max(r2.pontuacao) as maxp
      from resultados r2
      group by r2.nivel
     ) r2
     on u.nivel = r2.nivel and u.pontuacao = r2.maxp
WHER r.id_tipo = 1
GROUP BY u.nivel;

这会将最大值计算为子查询,然后将其重新加入。您可能不需要group by在此之后。此外,我修复了连接,以便它们使用正确的 ANSI 语法。

于 2013-08-27T16:09:58.150 回答