简体   繁体   English

具有多个条件的SqlAlchemy Case

[英]SqlAlchemy Case with multiple conditions

I am trying to do something like this: 我正在尝试做这样的事情:

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()

However its not picking up the second condition in case . 但是, case它没有处理第二个条件。 Is this possible? 这可能吗?

I got it working with and_ but its not giving the right answer 我让它与and_一起使用,但是没有给出正确的答案

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

should return 2, but its returning 0 应该返回2,但返回0

You need to use sqlalchemy.and_ instead of the and operator: 您需要使用sqlalchemy.and_而不是and运算符:

and_(Jobs.interview_type == 'PHONE_SCREEN',
     Jobs.interview_type == 'INCLINED')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM