首页 > mysql > 比较peewee sql Python中的日期时间

比较peewee sql Python中的日期时间 (Compare datetime in peewee sql Python)

问题

显然,我无法比较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条记录。但是,如果我datetime从sql中删除条件并进行如下比较

 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)

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

我需要做些什么来添加datetime条件peewee sql

非常感谢,

解决方法

显然,我无法比较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)))

问题

Apparently, I could not compare the date in the 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 )

Where Customers, Current_Insurers, Insurers are three class. The result is always 0 records. But if I removed the datetime condition from the sql and compare as follows

 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)

Surprisingly we can compare now and print out customers.

What do I need to do to add the datetime condition in the peewee sql?

Many thanks,

解决方法

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

That's completely incorrect. Do you honestly think that the library would be that broken??

The problem is Python operator precedence. You need to wrap the equality expressions with parentheses. So you where clause should look like this instead:

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

Additionally, it's typically only necessary to call switch() when you have multiple joins to a single model.

Put together, your query should be:

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)))
相似信息