我有下表:
+-----------+--------+------+--------+----------+----------+---------+-------+
| player_id | europe | asia | africa | namerica | lamerica | oceania | total |
+-----------+--------+------+--------+----------+----------+---------+-------+
| 121693 | 1076 | 70 | NULL | NULL | NULL | NULL | 1146 |
| 121693 | 416 | 70 | NULL | NULL | NULL | NULL | 486 |
| 121693 | 40 | NULL | NULL | NULL | NULL | NULL | 40 |
| 118840 | 40 | NULL | NULL | NULL | NULL | NULL | 40 |
| 121693 | 40 | 70 | NULL | NULL | NULL | NULL | 110 |
| 118840 | 40 | 490 | NULL | NULL | NULL | NULL | 530 |
| 121691 | 96 | 0 | NULL | NULL | NULL | NULL | 96 |
| 121693 | 136 | 70 | NULL | NULL | NULL | NULL | 206 |
| 118840 | 40 | NULL | NULL | NULL | NULL | NULL | 40 |
| 121691 | 96 | NULL | NULL | NULL | NULL | NULL | 96 |
+-----------+--------+------+--------+----------+----------+---------+-------+
我想得到的是:
+-----------+--------+------+--------+----------+----------+---------+-------+
| player_id | europe | asia | africa | namerica | lamerica | oceania | total |
+-----------+--------+------+--------+----------+----------+---------+-------+
| 121693 | 1076 | 70 | NULL | NULL | NULL | NULL | 1146 |
| 118840 | 40 | 490 | NULL | NULL | NULL | NULL | 530 |
| 121691 | 96 | 0 | NULL | NULL | NULL | NULL | 96 |
+-----------+--------+------+--------+----------+----------+---------+-------+
我尝试了以下内容,但结果并不是我所需要的。
SELECT player_id, europe, asia, africa, namerica,
lamerica, oceania, MAX(total) AS total
FROM tbl
WHERE total IS NOT NULL
GROUP BY player_id
ORDER BY total DESC
这是结果
+-----------+--------+------+--------+----------+----------+---------+-------+
| player_id | europe | asia | africa | namerica | lamerica | oceania | total |
+-----------+--------+------+--------+----------+----------+---------+-------+
| 121693 | 1076 | 70 | NULL | NULL | NULL | NULL | 1146 |
| 118840 | 40 | | NULL | NULL | NULL | NULL | 530 |
| 121691 | 96 | 0 | NULL | NULL | NULL | NULL | 96 |
+-----------+--------+------+--------+----------+----------+---------+-------+
请注意我使用的是MySQL视图。 MySQL中的视图不允许子查询。
修改
根据之前的建议,我试过这个:
SELECT a.player_id, a.europe, a.asia, a.africa, a.lamerica, a.namerica, a.oceania, a.total
FROM tbl AS a
INNER JOIN (
SELECT player_id, MAX(total) AS total
FROM tbl
GROUP BY player_id) b
ON a.player_id = b.player_id AND a.total = b.total
但我得到了这些结果:
+-----------+--------+------+--------+----------+----------+---------+-------+
| player_id | europe | asia | africa | lamerica | namerica | oceania | total |
+-----------+--------+------+--------+----------+----------+---------+-------+
| 121693 | 1076 | 70 | NULL | NULL | NULL | NULL | 1146 |
| 118840 | 40 | 490 | NULL | NULL | NULL | NULL | 530 |
| 121691 | 96 | NULL | NULL | NULL | NULL | NULL | 96 |
| 121691 | 96 | 0 | NULL | NULL | NULL | NULL | 96 |
+-----------+--------+------+--------+----------+----------+---------+-------+
它还返回平局。 如何避免这种情况?
答案 0 :(得分:0)
在查询中添加另一个GROUP BY a.player_id
:
SELECT a.player_id, a.europe, a.asia, a.africa, a.lamerica, a.namerica, a.oceania, a.total
FROM tbl AS a
INNER JOIN (
SELECT player_id, MAX(total) AS total
FROM tbl
GROUP BY player_id) b
ON a.player_id = b.player_id AND a.total = b.total
GROUP BY a.player_id
这将从带有关系的行中任意选择其他列的内容。
如果您启用了ONLY_FULL_GROUP_BY
SQL模式(默认情况下默认情况下从MySQL 5.7开始),您需要使用ANY_VALUE
来表明这是正常的。
SELECT a.player_id, ANY_VALUE(a.europe), ANY_VALUE(a.asia), ANY_VALUE(a.africa), ANY_VALUE(a.lamerica), ANY_VALUE(a.namerica), ANY_VALUE(a.oceania), ANY_VALUE(a.total)