PostgreSQL,从最大ID中选择 [英] PostgreSQL, SELECT from max id
本文介绍了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
这篇关于PostgreSQL,从最大ID中选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文