MySQL尚不支持Limit && in / all?

时间:2018-09-17 05:34:31

标签: mysql subquery limit

我的查询如下:

 SELECT * FROM `C_Institute`
 WHERE `ID` IN
 (SELECT `instituteID` FROM `C_Faculty` WHERE `ID` 
 IN (SELECT `facultyID` FROM `C_EducationalGroup` WHERE `ID` 
 IN (SELECT `educationalGroupID` FROM `C_StudyField` WHERE `ID` 
 IN (SELECT `studyFieldID` FROM `b_PersonEmployment` WHERE `personID`=1 ORDER BY `startDate` limit 1) )));

但是mysql不支持子查询上的limitin。我不知道如何编写此查询。 有人可以帮我吗?

预先感谢

3 个答案:

答案 0 :(得分:2)

您可以将此查询重写为一系列联接:

SELECT c1.*
FROM C_Institute c1
INNER JOIN C_Faculty c2
    ON c1.ID = c2.instituteID
INNER JOIN C_EducationalGroup c3
    ON c2.ID = c3.facultyID
INNER JOIN C_StudyField c4
    ON c3.ID = c4.educationalGroupID
INNER JOIN b_PersonEmployment b
    ON c4.ID = b.studyFieldID
WHERE b.personID = 1
ORDER BY b.startDate
LIMIT 1;

答案 1 :(得分:1)

假设每个表中的ID都引用了另一个表,

尝试一下,让我知道:

select * from C_Institute c join C_Faculty f on c.ID=F.instituteID left join C_EducationalGroup e on f.ID=e.facultyID left join C_StudyField sf on e.ID=sf.educationalGroupID left join b_PersonEmployment p on sf.ID=p.studyFieldID where p.personID=1 order by p.startDate limit 1;  

此外,分享您的表描述以获取更多线索

答案 2 :(得分:0)

感谢您的回答。但是我认为加入不是解决此问题的好方法。我已经通过两个查询完成了(也许不是个好主意,但总比加入更好):

rm -rf .git/refs/remotes/origin/angular_removal