SELECT IFNULL( count( nt.id ), 0 ) total, IFNULL( count( ta.id ), 0 ) endtotal, cs.taskname,nt.id,nt.name FROM nk_xjsg_taskresult nt JOIN nk_xjsg_constask cs ON cs.id = nt.taskid AND cs.customerid = nt.customerid AND cs.pid = '40289e8c87fff7f80188042c15d50010' JOIN nk_xjsg_consplan nc ON nc.id = cs.pid AND nc.customerid = cs.customerid LEFT JOIN ( SELECT id, resultid, statusnum, statusname, endnum FROM nk_xjsg_taskresult_date WHERE customerid = '40289e8c8803a391018803a6ef8f0000' ) ta ON ta.resultid = nt.id and ta.statusnum = '40' -- AND ta.number = 1 -- -- AND ta.endnum = nt.resultnum WHERE nt.customerid = '40289e8c8803a391018803a6ef8f0000' GROUP BY cs.taskname ,nt.id ,nt.name; 为什么第一个total是5 不应该是1吗 怎么修改成1
时间: 2024-04-06 10:32:24 浏览: 8
根据你提供的SQL查询语句,第一个total是通过COUNT函数统计的nt.id的数量,因此结果为5。如果你想让第一个total只统计一次nt.id的数量,可以将COUNT函数的参数改为DISTINCT,即将“COUNT(nt.id)”改为“COUNT(DISTINCT nt.id)”。这样就可以确保只统计nt.id的不同值,从而得到正确的结果。修改后的SQL查询语句如下:
SELECT IFNULL(COUNT(DISTINCT nt.id), 0) total, IFNULL(COUNT(ta.id), 0) endtotal, cs.taskname, nt.id, nt.name
FROM nk_xjsg_taskresult nt
JOIN nk_xjsg_constask cs ON cs.id = nt.taskid AND cs.customerid = nt.customerid AND cs.pid = '40289e8c87fff7f80188042c15d50010'
JOIN nk_xjsg_consplan nc ON nc.id = cs.pid AND nc.customerid = cs.customerid
LEFT JOIN (
SELECT id, resultid, statusnum, statusname, endnum
FROM nk_xjsg_taskresult_date
WHERE customerid = '40289e8c8803a391018803a6ef8f0000'
) ta ON ta.resultid = nt.id and ta.statusnum = '40' -- AND ta.number = 1 -- AND ta.endnum = nt.resultnum
WHERE nt.customerid = '40289e8c8803a391018803a6ef8f0000'
GROUP BY cs.taskname, nt.id, nt.name;