我有多个表,我想检查分配给用户的文件数量,他今天没有处理过这些文件。我已经写了下面的查询,但我得到了一个错误
SELECT t2.id as id,t2.task_date,t3.id as debtor_id,t1.cfid as cfid, t2.cfid,t2.case_file,t2.scheduled_action,t2.debtor
FROM task_list t2
INNER JOIN case_files t1
ON t2.cfid=t1.cfid
INNER JOIN case_file_allocations t3
ON t2.cfid=t3.cfid
WHERE t2.worked<>'yes'
AND MAX(t2.task_date) < NOW()
AND t2.acm=t3.acm
AND t2.acm='310'
AND t2.deleted<>1
AND t1.closed<>'yes'
AND t1.deleted<>1
AND t1.pool_id IN(0,1)
AND t3.reallocated<>'yes'
ORDER BY t2.task_date ASC
请为我检查错误
答案 0 :(得分:0)
您不能在where
子句中使用聚合函数。如果你想这样做,你需要:
group by
子句)having
子句中的分组或聚合字段中。 select
查询的正确语法是:
SELECT -- Your fields and/or expressions
FROM -- Your data sources (tables, views and/or subqueries), and joins
WHERE -- Conditions on the "raw" data
GROUP BY -- Grouping fields
HAVING -- Conditions on grouped and/or aggregated data
ORDER BY -- Ordering fields
如果您想要最后task_list
为24小时的task_date
条记录:
select id, max(task_date) as max_task_date
from task_list
where
task_date <= date_add(now(), interval -1 day)
-- maybe you need something like: task_date <= date_add(curdate(), interval -1 day)
group by id;
我留给你剩下的......你可以把这个结果和你的其他桌子一起来获得你需要的信息。
答案 1 :(得分:0)
使用子查询获取每个案例文件的最大日期,然后计算外部查询中符合条件的数量:
select count(*)
from (
SELECT t1.cfid as cfid, MAX(t2.task_date) d
FROM task_list t2
INNER JOIN case_files t1 ON t2.cfid=t1.cfid
INNER JOIN case_file_allocations t3 ON t2.cfid=t3.cfid
WHERE t2.worked<>'yes' AND t2.acm=t3.acm AND t2.acm='310' AND t2.deleted<>1
AND t1.closed<>'yes' AND t1.deleted<>1 AND t1.pool_id IN(0,1)
AND t3.reallocated<>'yes'
group by t1.cfid) a
where d < NOW();