首页 > 解决方案 > 具有多个条件的 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

标签: pythonsqlalchemy

解决方案


您需要使用sqlalchemy.and_而不是and运算符:

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

推荐阅读