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, ROW_NUMBER() over ( PARTITION BY resultid ORDER BY createdate DESC ) number FROM nk_xjsg_taskresult_date WHERE customerid = '40289e8c8803a391018803a6ef8f0000' ) ta ON ta.resultid = nt.id AND ta.number = 1 -- -- AND ta.endnum = nt.resultnum WHERE nt.customerid = '40289e8c8803a391018803a6ef8f0000' GROUP BY cs.taskname ,nt.id ,nt.`name`;我nk_xjsg_taskresult_date 有两天数据 为什么只查询出来一条 什么原因 怎么解决
时间: 2024-03-16 14:47:09 浏览: 22
根据你提供的 SQL 查询语句,可以看出使用了子查询来查询 `nk_xjsg_taskresult_date` 表中的数据,并使用了 `ROW_NUMBER()` 函数来获取每个 `resultid` 最新的一条数据。而在子查询中,使用了 `PARTITION BY resultid` 来对数据进行分组,所以每个 `resultid` 只会返回一条数据。
如果你想要查询 `nk_xjsg_taskresult_date` 表中所有的数据,可以将子查询中的 `PARTITION BY resultid` 移除,这样就不会对数据进行分组,从而返回所有的数据。修改后的 SQL 查询语句如下:
```
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,
ROW_NUMBER() over (ORDER BY createdate DESC) number
FROM
nk_xjsg_taskresult_date
WHERE
customerid = '40289e8c8803a391018803a6ef8f0000'
) ta ON ta.resultid = nt.id
AND ta.number = 1
WHERE
nt.customerid = '40289e8c8803a391018803a6ef8f0000'
GROUP BY
cs.taskname,
nt.id,
nt.`name`;
```
这样就可以查询出 `nk_xjsg_taskresult_date` 表中的所有数据了。