我在mysql中有以下表格,它是一个真正问题的简化版本。如何在聚合后保留额外的列?
+-------+-------+-------+-------+
| a_col | b_col | c_col | extra |
+-------+-------+-------+-------+
| 1 | 1 | 1 | 7 |*
| 1 | 2 | 1 | 10 |
| 1 | 2 | 2 | 20 |*
| 1 | 3 | 2 | 20 |
| 1 | 3 | 3 | 15 |*
+-------+-------+-------+-------+
我要选择标有*的行,下面的表中获取:
+-------+-------+-------+-------+
| a_col | b_col | c_col | extra |
+-------+-------+-------+-------+
| 1 | 1 | 1 | 7 |
| 1 | 2 | 2 | 20 |
| 1 | 3 | 3 | 15 |
+-------+-------+-------+-------+
如果两行在a_col和b_col相同的值,然后继续谁具有最大的一个在c_col值
我的尝试是:
select a_col, b_col, max(c_col), extra
from mytable
group by a_col, b_col
,但我得到了以下错误消息:
ERROR 1055 (42000): Expression #4 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'bd17_g12.mytable.extra' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
而不提“额外”的专栏中,我得到的东西接近我的愿望:
+-------+-------+------------+
| a_col | b_col | max(c_col) |
+-------+-------+------------+
| 1 | 1 | 1 |
| 1 | 2 | 2 |
| 1 | 3 | 3 |
+-------+-------+------------+
但我需要保持“额外”列的值