过滤子查询中的多个列

时间:2014-10-01 15:13:39

标签: python sqlalchemy subquery flask-sqlalchemy

我需要能够过滤子查询中的多列行

在sql中,这就像

where (account_details.account_detail_id, tasks.base_action_type_id)  IN 
(
SELECT tasks.account_detail_id  , tasks.base_action_type_id
FROM tasks 
WHERE tasks.dt_created > now() - interval '1 day'
GROUP BY tasks.account_detail_id ,tasks.base_action_type_id
HAVING count(tasks.account_detail_id) < 3
) 

但是我找不到有关sqlalchmey

格式的任何信息

我试过

dayBefore = datetime.utcnow() - timedelta(hours=24)

subquery = Tasks.query.with_entities(Tasks.account_detail_id, Tasks.base_action_type_id).\
    filter(Tasks.dt_created >  dayBefore).\
    group_by(Tasks.account_detail_id, Tasks.base_action_type_id).\
    having(func.count(Tasks.account_detail_id) < 3)

sql = AccountDetails.query.join(Tasks, Tasks.account_detail_id == AccountDetails.account_detail_id).\
    filter(AccountDetails.account_detail_id == 3,  (AccountDetails.account_detail_id,Tasks.base_action_type_id).in_(subquery))

注意这一行

(AccountDetails.account_detail_id,Tasks.base_action_type_id).in_(subquery)

但这只是错误的无效语法

1 个答案:

答案 0 :(得分:0)

像往常一样,发布前几小时的搜索一无所获。发布问题后首先搜索找到答案

Using sqlalchemy to query using multiple column where in clause