gpt4 book ai didi

sql - 如何从sql查询中获取第一个和最后一个记录?

转载 作者:行者123 更新时间:2023-11-29 11:05:33 25 4
gpt4 key购买 nike

在 PostgreSQL 中,我使用多个条件对其运行查询,返回多行,按其中一列排序。示例:

SELECT <some columns> 
FROM mytable
<maybe some joins here>
WHERE <various conditions>
ORDER BY date DESC

如何从该查询中获取第一行和最后一行?

最佳答案

[警告:可能不是最有效的方法]:

(SELECT <some columns>
FROM mytable
<maybe some joins here>
WHERE <various conditions>
ORDER BY date DESC
LIMIT 1)

UNION ALL

(SELECT <some columns>
FROM mytable
<maybe some joins here>
WHERE <various conditions>
ORDER BY date ASC
LIMIT 1)

关于sql - 如何从sql查询中获取第一个和最后一个记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1485391/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com