init
This commit is contained in:
30
app/crud/form_crud.py
Normal file
30
app/crud/form_crud.py
Normal file
@@ -0,0 +1,30 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding:utf-8 -*-
|
||||
|
||||
from sqlalchemy import and_, func
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.models.form import Form
|
||||
from app.schemas import FormCreate, FormUpdate
|
||||
from app.crud.base import CRUDBase
|
||||
|
||||
|
||||
class CRUDForm(CRUDBase[Form, FormUpdate, FormCreate]):
|
||||
def get_last_bench(self, db: Session, num: int = 10):
|
||||
# 创建子查询,获取每个不同的name的最大id
|
||||
subquery = (
|
||||
db.query(self.model.name, func.max(self.model.id).label('max_id'))
|
||||
.group_by(self.model.name)
|
||||
.subquery()
|
||||
)
|
||||
|
||||
# 主查询,与子查询连接,获取每个name的最后一条数据
|
||||
results = (
|
||||
db.query(self.model)
|
||||
.join(subquery, and_(self.model.id == subquery.c.max_id))
|
||||
.all()
|
||||
)
|
||||
return results
|
||||
|
||||
|
||||
form = CRUDForm(Form)
|
||||
Reference in New Issue
Block a user