具有多个条件的SqlAlchemy Case

时间:2019-04-18 15:23:10

标签: python sqlalchemy

我正在尝试做这样的事情:

x = db_session.query(
    Candidate,
    func.count(case([(Jobs.interview_type == 'PHONE_SCREEN' and Jobs.interview_type == 'INCLINED' , 1)])),
    func.count(case([(Jobs.interview_type == 'PHONE_SCREEN' and Jobs.interview_type == 'NOT_INCLINED', 1)])),
    func.count(case([(Jobs.interview_type == 'IN_HOUSE', 1)])),
    func.count(case([(Jobs.interview_type == 'EVALUATION', 1)]))
    ).\
    join(Jobs, Jobs.candidate_id == Candidate.candidate_id).\
    filter(Candidate.candidate_id == '6236738').\
    first()

但是它没有处理case中的第二个条件。这可能吗?

我让它与and_一起工作,但是给出的答案不正确

func.count(case([(and_(Jobs.interview_type == 'PHONE_SCREEN', Jobs.interview_type == 'INCLINED'), 1)])),

应返回2,但其返回0

1 个答案:

答案 0 :(得分:1)

您需要使用sqlalchemy.and_代替and运算符:

and_(Jobs.interview_type == 'PHONE_SCREEN',
     Jobs.interview_type == 'INCLINED')
相关问题