[英]How to make distinct via peewee in the python language?
I have a table in the sqlite with column "telegram_id" I want to get all unique records of this column我在 sqlite 中有一个表,其中包含“telegram_id”列我想获取此列的所有唯一记录
def get_all_telegram_ids():
'''Returns all telegram_id from DB'''
#rows = cursor.execute("SELECT DISTINCT telegram_id FROM locations").fetchall()
rows = Locations.select(Locations.telegram_id).distinct()
print(rows)
return rows
but this code does not work:-(但是这段代码不起作用:-(
Seems to be working fine:似乎工作正常:
import random
from peewee import *
db = SqliteDatabase(':memory:')
class Location(Model):
telegram_id = IntegerField()
class Meta:
database = db
db.create_tables([Location])
for i in range(100):
Location.create(telegram_id=random.randint(1, 10))
q = Location.select(Location.telegram_id).distinct()
for l in q:
print(l.telegram_id)
Prints 1-10 with some variation and no duplicates.打印 1-10 有一些变化,没有重复。
Alternative invocation that also works:也有效的替代调用:
q = Location.select(fn.DISTINCT(Location.telegram_id))
for l in q:
print(l.telegram_id)
but this code does not work:-(
但是这段代码不起作用:-(
Have you considered reading the documentation您是否考虑过阅读文档
By specifying a single value of
True
the query will use a simpleSELECT DISTINCT
.通过指定单个
True
值,查询将使用简单的SELECT DISTINCT
。 Specifying one or more columns will result in aSELECT DISTINCT ON
.指定一个或多个列将导致
SELECT DISTINCT ON
。
So it doesn't seem like you can just call distinct()
with no parameters, it's always at least select(True)
.所以看起来你不能只调用不带参数的
distinct()
,它总是至少select(True)
。
Also when asking questions about things not working, it's usually a good idea to explain the ways in which it is not working eg the error you get if there is one, or your expectations versus what you're observing.此外,当询问有关不工作的问题时,通常最好解释它不工作的方式,例如,如果有错误,你得到的错误,或者你的期望与你观察到的结果。
Even more so when you don't come close to providing a minimal reproducible example .更何况当你没有接近提供一个最小的可重现的例子时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.