gpt4 book ai didi

mysql - 比较peewee sql Python中的日期时间

转载 作者:行者123 更新时间:2023-11-29 05:16:08 26 4
gpt4 key购买 nike

显然,我无法比较 peewee SQL 中的日期。

START_DATE = datetime.datetime(2015, 7, 20, 0, 0, 0)    
customer_records = Customers.select().\
join(Current_Insurers, on=(Customers.current_insurer == Current_Insurers.id)).\
switch(Current_Insurers).\
join(Insurers, on=(Current_Insurers.insurer == Insurers.id)).\
where(Customers.pol_type == "PC" & \
Current_Insurers.effective_date == START_DATE )

其中 CustomersCurrent_InsurersInsurers 是三个 class。结果总是 0 条记录。但是,如果我从 sql 中删除 datetime 条件并进行如下比较

 customer_records = Customers.select().\
join(Current_Insurers, on=(Customers.current_insurer == Current_Insurers.id)).\
switch(Current_Insurers).\
join(Insurers, on=(Current_Insurers.insurer == Insurers.id)).\
where(Customers.pol_type == "PC"
for r in customer_records:
if(r.current_insurer.effective_date == START_DATE):
print(r.policy_id)

令人惊讶的是,我们现在可以比较并打印出客户。

peewee sql中添加datetime条件需要做什么?

非常感谢,

最佳答案

Apparently, I could not compare the date in the peewee SQL.

这是完全不正确的。老实说,您认为图书馆会被破坏吗??

问题是 Python 运算符优先级。您需要用括号将相等表达式括起来。所以你的 where 子句应该看起来像这样:

where((Customers.pol_type == "PC") & \
(Current_Insurers.effective_date == START_DATE))

此外,通常只有在对单个模型进行多个连接时才需要调用 switch()

放在一起,您的查询应该是:

query = (Customers
.select()
.join(Current_Insurers, on=(Customer.current_insurer == Current_Insurers.id))
.join(Insurers, on=(Current_Insurers.insurer == Insurer.id))
.where(
(Customers.pol_type == "PC") &
(Current_Insurers.effective_date == START_DATE)))

关于mysql - 比较peewee sql Python中的日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32563411/

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