通过优化计算项目的子查询来实现斗争:
SELECT @crewid:=crewid AS `crewid`, COUNT(id) AS `number_documents`,
(SELECT COUNT(id) FROM tblScan WHERE classification IS NOT NULL AND crewId = @crewid) AS `classified`,
(SELECT COUNT(id) FROM tblScan WHERE classification IS NULL AND crewId = @crewid) AS `unclassified`,
(SELECT CONCAT(name, ' ', surname) FROM tblcrew WHERE crewId = @crewid) AS `crewname`,
(SELECT uploadeddate FROM tblScan WHERE crewid = @crewid ORDER BY uploadeddate DESC LIMIT 0,1) AS `uploadeddate`,
(SELECT uploadedby FROM tblScan WHERE crewid = @crewid ORDER BY uploadedby DESC LIMIT 0,1) AS `uploadedby`,
(SELECT updateddate FROM tblScan WHERE crewid = @crewid AND updateddate IS NOT NULL ORDER BY updateddate DESC LIMIT 0,1) AS `updateddate`,
(SELECT updatedby FROM tblScan WHERE crewid = @crewid AND updatedby IS NOT NULL ORDER BY updatedby DESC LIMIT 0,1) AS `updatedby`
FROM tblScan
希望有人可以提供帮助
谢谢,
答案 0 :(得分:2)
SELECT
`tblScan`.`crewId` AS `crewid`
COUNT(`tblScan`.`id`) AS `number_documents`,
SUM(IF(`tblScan`.`classification` IS NOT NULL,1,0)) AS `classified`,
SUM(IF(`tblScan`.`classification` IS NULL,1,0)) AS `unclassified`,
CONCAT(`tblcrew`.`name`, ' ', `tblcrew`.`surname`) AS `crewname`,
MAX(`tblScan`.`uploadeddate`) AS `uploadeddate`,
MAX(`tblScan`.`uploadedby`) AS `uploadedby`,
MAX(IFNULL(`tblScan`.`updateddate`,'0000-01-01')) AS `updateddate`,
MAX(IFNULL(`tblScan`.`updatedby`,'0')) AS `updatedby`
FROM tblScan
INNER JOIN tblcrew ON `tblcrew`.`crewId`=`tblScan`.`crewId`
GROUP BY `tblScan`.`crewId`