2010-10-19 67 views
4

有誰知道我可以做計數如果SQL鍊金術一樣SQLAlchemy的COUNT和IF

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

我做這樣的事情,

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

但是,如果與TransactionMesssage.tm_read的SQLAlchemy讓2個獨立的, 和TransactinMessage.tm_type

可以somone幫我解決問題嗎?

回答

9

我沒有環境下測試,但最有可能你需要使用sqlalchemy.sql.expression.and_表達:

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

感謝對幫助,它的工作! – gummmibear 2010-10-20 20:40:10

+0

感謝您的支持。它在SQLAlchemy站點中沒有記錄。 – fccoelho 2011-03-11 17:51:43