开发者

sqlalchemy COUNT and IF

开发者 https://www.devze.com 2023-01-21 14:13 出处:网络
anyone know how can i do counting if in SQL alchemy like COUN(IF(table_row = 1 AND table_row2 =2),1,0)

anyone know how can i do counting if in SQL alchemy like

COUN(IF(table_row = 1 AND table_row2 =2),1,0)

i make something like this,

func.COUNT(func.IF((TransactionMessage.tm_read==0 and TransactionMessage.tm_type==1),1,0)).label('t_message_count'),

But sqlalchemy make 2 seperate if with TransactionMesssage.tm_read, and TransactinMessage.tm_type

Could som开发者_运维问答one help me to resolve problem?


I do not have environment to test, but most probably you need to use sqlalchemy.sql.expression.and_ expression:

from sqlalchemy.sql.expression import and_
...
func.COUNT(func.IF(and_(TransactionMessage.tm_read == 0, 
                        TransactionMessage.tm_type == 1), 1, 0)
           ).label('t_message_count'),
0

精彩评论

暂无评论...
验证码 换一张
取 消