没有子查询的左连接
Left join Without Subqueries
我得到以下输出,我想要相同的输出但没有子查询。我希望通过删除子查询来优化下面的查询,因为它会影响应用程序的性能。谁能帮我删除子查询并使用连接获取所需的数据。
SELECT DISTINCT rec.requestno as requestno
, (SELECT min(dateandtime)
from recruitersubmission
where recruitersubmission.requestno = rec.requestno) as firstsubmsion
, (SELECT max(dateandtime)
from recruitersubmission
where recruitersubmission.requestno = rec.requestno) as lastsubmission
, cndinterview.dateandtime as candidatefedtime
, cndinterview.dateandtime AS recruitertime
, CONCAT(availabledate,' ', availabletime) AS candidateavaibledatetime
, cndfeedback.status AS status
, cndfeedback.offereddate AS offereddate
, cnd.status AS onboard
, (SELECT COUNT(recruitersubmission.requestno)
FROM recruitersubmission
WHERE recruitersubmission.requestno=rec.requestno) AS totalSub
, (select COUNT(candidatefeedback.requestno)
from candidatefeedback
WHERE candidatefeedback.requestno = rec.requestno
&& candidatefeedback.status = 'Selected' ) as totalsel
, (select COUNT( candidatefeedback.requestno)
from candidatefeedback
WHERE candidatefeedback.requestno = rec.requestno
&& cndfeedback.status = 'Rejected' ) as totalrej
, (select COUNT(candidatefeedbkonboard.requestno)
from candidatefeedbkonboard
WHERE candidatefeedbkonboard.requestno = rec.requestno
&& cnd.status = 'Drop' ) as totaldrop
, (select COUNT(candidatefeedbkonboard.requestno)
from candidatefeedbkonboard
WHERE candidatefeedbkonboard.requestno = rec.requestno
&& candidatefeedbkonboard.status = 'Onboarded' ) as totalonboard
from recruitersubmission AS rec
LEFT JOIN candidatefeedbkonboard AS cnd
ON rec.requestno=cnd.requestno
LEFT JOIN candidatefeedback AS cndfeedback
ON rec.requestno=cndfeedback.requestno
LEFT JOIN candidatesinterview AS cndinterview
ON rec.requestno=cndinterview.requestno
where rec.clientname = '$client'
&& rec.requestno != ''
&& rec.country = '$location'
&& date(rec.dateandtime) between '$fromdate'
我想避免下面这些子查询。
(select COUNT(candidatefeedback.requestno) 来自 candidatefeedback WHERE candidatefeedback.requestno = rec.requestno && candidatefeedback.status = 'Selected' ) as totalsel
感谢朋友们的帮助。我想出了怎么做。下面是解决方法供大家参考。
SELECT
rec.requestno,
COALESCE(totalsub, 0) AS totalsub,
COALESCE(totalsel, 0) AS totalsel,
COALESCE(totalrej, 0) AS totalrej
FROM recruitersubmission rec
LEFT JOIN (
SELECT requestno, COUNT(*) AS totalsub
FROM recruitersubmission
GROUP BY requestno
) recruitersubmission ON recruitersubmission.requestno = rec.requestno
LEFT JOIN (
SELECT requestno,count(if(status = 'Selected',1,Null)) AS totalsel,count(if(status = 'Rejected',1,Null)) as totalrej
FROM candidatefeedback
GROUP BY requestno
) candidatefeedback ON candidatefeedback.requestno = rec.requestno
GROUP by rec.requestno Limit 0,25
回复:您的子查询:
(SELECT 分钟(日期和时间)
来自招聘人员提交
其中 recruitersubmission.requestno = rec.requestno)
需要INDEX(requestno, dateandtime)
。对于其他子查询也是如此。
回复:Seejith 的回答:
count(if(status = 'Selected',1,Null))
可以替换为
SUM(status = 'Selected')
从而无需 COALESCE(.., 0)
。
我得到以下输出,我想要相同的输出但没有子查询
SELECT DISTINCT rec.requestno as requestno
, (SELECT min(dateandtime)
from recruitersubmission
where recruitersubmission.requestno = rec.requestno) as firstsubmsion
, (SELECT max(dateandtime)
from recruitersubmission
where recruitersubmission.requestno = rec.requestno) as lastsubmission
, cndinterview.dateandtime as candidatefedtime
, cndinterview.dateandtime AS recruitertime
, CONCAT(availabledate,' ', availabletime) AS candidateavaibledatetime
, cndfeedback.status AS status
, cndfeedback.offereddate AS offereddate
, cnd.status AS onboard
, (SELECT COUNT(recruitersubmission.requestno)
FROM recruitersubmission
WHERE recruitersubmission.requestno=rec.requestno) AS totalSub
, (select COUNT(candidatefeedback.requestno)
from candidatefeedback
WHERE candidatefeedback.requestno = rec.requestno
&& candidatefeedback.status = 'Selected' ) as totalsel
, (select COUNT( candidatefeedback.requestno)
from candidatefeedback
WHERE candidatefeedback.requestno = rec.requestno
&& cndfeedback.status = 'Rejected' ) as totalrej
, (select COUNT(candidatefeedbkonboard.requestno)
from candidatefeedbkonboard
WHERE candidatefeedbkonboard.requestno = rec.requestno
&& cnd.status = 'Drop' ) as totaldrop
, (select COUNT(candidatefeedbkonboard.requestno)
from candidatefeedbkonboard
WHERE candidatefeedbkonboard.requestno = rec.requestno
&& candidatefeedbkonboard.status = 'Onboarded' ) as totalonboard
from recruitersubmission AS rec
LEFT JOIN candidatefeedbkonboard AS cnd
ON rec.requestno=cnd.requestno
LEFT JOIN candidatefeedback AS cndfeedback
ON rec.requestno=cndfeedback.requestno
LEFT JOIN candidatesinterview AS cndinterview
ON rec.requestno=cndinterview.requestno
where rec.clientname = '$client'
&& rec.requestno != ''
&& rec.country = '$location'
&& date(rec.dateandtime) between '$fromdate'
我想避免下面这些子查询。
(select COUNT(candidatefeedback.requestno) 来自 candidatefeedback WHERE candidatefeedback.requestno = rec.requestno && candidatefeedback.status = 'Selected' ) as totalsel
感谢朋友们的帮助。我想出了怎么做。下面是解决方法供大家参考。
SELECT
rec.requestno,
COALESCE(totalsub, 0) AS totalsub,
COALESCE(totalsel, 0) AS totalsel,
COALESCE(totalrej, 0) AS totalrej
FROM recruitersubmission rec
LEFT JOIN (
SELECT requestno, COUNT(*) AS totalsub
FROM recruitersubmission
GROUP BY requestno
) recruitersubmission ON recruitersubmission.requestno = rec.requestno
LEFT JOIN (
SELECT requestno,count(if(status = 'Selected',1,Null)) AS totalsel,count(if(status = 'Rejected',1,Null)) as totalrej
FROM candidatefeedback
GROUP BY requestno
) candidatefeedback ON candidatefeedback.requestno = rec.requestno
GROUP by rec.requestno Limit 0,25
回复:您的子查询:
(SELECT 分钟(日期和时间) 来自招聘人员提交 其中 recruitersubmission.requestno = rec.requestno)
需要INDEX(requestno, dateandtime)
。对于其他子查询也是如此。
回复:Seejith 的回答:
count(if(status = 'Selected',1,Null))
可以替换为
SUM(status = 'Selected')
从而无需 COALESCE(.., 0)
。