我有以下表格:
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