[英]How to convert this query to the Peewee ORM?
我正在使用(優秀) Peewee ORM來滿足我在Python中的查詢需求,現在我想轉換以下查詢:
select t1.created, t1.property_id, t1.requesting_user_id, t1.type, t1.response
from pdr t1
inner join (
select max(id) as id, property_id, requesting_user_id
from pdr
where property_id = 7
group by requesting_user_id
) as t2 on t2.id = t1.id
因此,我提出了以下建議:
PDR.select()\
.join(PDR)\
.where(PDR.property == 7)\
.group_by(PDR.requesting_user)
但這會創建以下sql:
SELECT t1.id, t1.created, t1.property_id, t1.requesting_user_id, t1.type, t1.comment, t1.responding_user_id, t1.property_details_request_id, t1.response
FROM pdr AS t1
INNER JOIN pdr AS t1
ON (t1.property_details_request_id = t1.id)
WHERE (t1.property_id = 7)
GROUP BY t1.requesting_user_id
我嘗試了其他幾種變體,但是有點卡住了。
有人知道如何將查詢轉換為Peewee嗎? 歡迎所有提示!
嘗試以下操作(未經測試,但希望會有所幫助):
PDRAlias = PDR.alias()
subq = (PDRAlias
.select(fn.MAX(PDRAlias.id).alias('max_id'), PDRAlias.property, PDRAlias.requesting_user)
.where(PDRAlias.property == 7)
.group_by(PDRAlias.requesting_user)
.alias('subq'))
query = (PDR
.select()
.join(subq, on=(subq.c.max_id == PDR.id)))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.