gpt4 book ai didi

python - 从 Peewee 的列中选择不同的值

转载 作者:太空狗 更新时间:2023-10-30 02:43:00 24 4
gpt4 key购买 nike

我希望使用 Peewee 从一列中选择所有不同的值。

例如如果我有 table

 Organization     Year   
company_1 2000
company_1 2001
company_2 2000
....

只返回组织列中的唯一值 [即 company_1company_2]

我曾假设使用 distinct 选项可以实现 http://docs.peewee-orm.com/en/latest/peewee/api.html#SelectQuery.distinct

我当前的代码:

   organizations_returned = organization_db.select().distinct(organization_db.organization_column).execute()

for item in organizations_returned:
print (item.organization_column)

不会导致返回不同的行(它会导致例如 company_1 两次)。

我试过的另一种选择:

  organization_db.select().distinct([organization_db.organization_column]).execute()

disctinct 选项中包含 [ ],虽然看起来与文档更一致,但导致了错误 peewee.OperationalError: near "ON ": 语法错误:

我假设可以直接从 Peewee 返回唯一值是否正确 - 如果是这样,我做错了什么?


模型结构:

cd_sql = SqliteDatabase(sql_location, threadlocals=True, pragmas=(("synchronous", "off"),))     

class BaseModel(Model):
class Meta:
database = cd_sql

class organization_db(BaseModel):
organization_column = CharField()
year_column = CharField()

最佳答案

所以 coleifer 的意思是 Sqlite 不支持 DISTINCT ON。不过这不是什么大问题,我认为你可以像这样完成你想要的:

organization_db.select(organization_db.organization).distinct()

关于python - 从 Peewee 的列中选择不同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34823172/

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