目前我需要使用此查询:
SELECT name FROM persons WHERE persons."valid"=TRUE GROUP BY "name" ORDER BY name ASC LIMIT 10;
我需要将 group_by 添加到我的Sqlalchemy:
persons = Model.Person.query.with_entities(Model.Person.name).filter(
Model.Person.valid.is_(True)).order_by(
asc(Model.Person.name)).limit(settings.items_per_page).all()
我需要添加group_by,因为我的结果包含重复的选项:
{
"persons": [
{
"name": "Akshay"
},
{
"name": "Akshay Navle"
},
{
"name": "Alexa"
},
{
"name": "Alexa"
},
{
"name": "Alexa"
},
{
"name": "Alexa"
},
{
"name": "Alexa"
},
{
"name": "Alexa"
},
{
"name": "Americans"
},
{
"name": "Americans"
}
]
}
我试过了:
persons = Model.Person.query.with_entities(Model.Person.name).filter(
Model.Person.valid.is_(True)).order_by(
asc(Model.Person.name)).limit(settings.items_per_page).group_by(Model.Person.name).all()
但是得到空结果,有什么建议吗?