PostgreSQL,从最大ID中选择
问题描述:
通过在PG 9.1上使用libpq,我试图编写查询以从具有最高索引'my_id'的行获取值:
By using libpq on PG 9.1, I am trying to write query to get values from row with highest index 'my_id':
SELECT my_id, col2, col3
FROM mytable
WHERE my_id = MAX(my_id)
这给了我错误:
错误:WHERE子句中不允许聚合...
ERROR: aggregates not allowed in WHERE clause...
如何正确编写此类查询?
How to write such query properly?
答
如果您的目标是是获取具有最高my_id值的行,则以下查询应达到相同的目标。
If your goal is to get the row with the highest my_id value, then the following query should achieve the same goal.
SELECT my_id, col2, col3
FROM mytable
ORDER BY my_id DESC
LIMIT 1