Cucco’s Compute Hack

コンピュータ関係の記事を書いていきます。

SQL操作で列の最大値を得る

ある列が最大の列の値を取ってくる場合、以下は間違い。

mysql> select id,time from table_sec where time=max(time);
ERROR 1111 (HY000): Invalid use of group function

列名に対してMAXを適用するなんて不思議な感じ。

mysql> select id,max(time) from table_sec;
+----+---------------------+
| id | max(time)           |
+----+---------------------+
|  1 | 2016-12-12 11:00:15 |
+----+---------------------+
1 row in set (0.00 sec)