mysql还不支持limit&&in/all?

ngynwnxp  于 2021-06-21  发布在  Mysql
关注(0)|答案(3)|浏览(323)

我的问题如下:

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不支持 limit 以及 in 在子查询上。我不知道怎么写这个查询。有人能帮我吗?
提前谢谢

92vpleto

92vpleto1#

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

SET @studyFieldID =(SELECT `studyFieldID` FROM `b_PersonEmployment` WHERE `personID`=1 ORDER BY `startDate` limit 1); 
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`=@studyFieldID 
  )));
ajsxfq5m

ajsxfq5m2#

假设每个表中的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;

另外,分享你的表格描述以获得更多线索

ubof19bj

ubof19bj3#

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

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;

相关问题