python - 如何在 sqlalchemy 中编写不同的查询?
问题描述
我有一个 SQL 查询,我正在尝试将其转换为 sqlalchemy,但它不一样。这是查询:
select distinct on (uid) uid, id, amount, type, due_date
from events
where status = 'pending' and due_date <= '2020-09-14'
order by uid, due_date desc;
这就是我尝试在 sqlalchemy 中编写它的方式:
today = datetime.today()
query = Events.select().distinct(Events.c.uid).where(
Events.c.status == EventStatus.pending).where(
Events.c.due_date <= today).order_by(
Events.c.uid, Events.c.due_date.desc())
当我打印上面的查询时,我得到:
SELECT DISTINCT events.id, events.uid, events.type, events.status,
events.amount, events.old_tier, events.new_tier, events.due_date,
events.created_at, events.updated_at
FROM events
WHERE events.status = :status_1 AND events.due_date <= :due_date_1
ORDER BY events.uid, events.due_date DESC
这不是一回事,因为我原来的查询是DISTINCT ON
. 这里有什么问题?PS:我正在使用 postgres
解决方案
推荐阅读
- json - TCL 字典转 JSON
- java - 在 Hibernate 中替换缺少接口支持的解决方法是什么?
- python - pm4py 错误:无法从“pm4py.algo.discovery.alpha”导入名称“工厂”
- api - 使用 API 查找 URL 在 Twitter 中出现的总时间
- c# - C# 进程试图执行“移动”命令
- swift - 为什么我的 SwiftUI Grid 不适应可用空间?
- c# - 如何确定哪个键触发了 RoutedCommand 事件
- spring-cloud-gateway - 每次请求都会调用 Resilience4j 断路器回退
- node.js - npm 错误!cb.apply 不是函数(基本操作系统)
- c# - C# 多线程调用数据网格的更新 GUI 无法正常工作