MySQL还不支持限制&& in / all?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL还不支持限制&& in / all?相关的知识,希望对你有一定的参考价值。

我的查询如下:

 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。我不知道怎么写这个查询。有人可以帮我吗?

提前致谢

答案

您可以重写此查询一系列连接:

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;
另一答案

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

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

另一答案

谢谢你的所有答案。但我认为加入并不是解决这个问题的好方法。我通过两个查询完成它(可能不是好主意但比连接更好):

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 
  )));

以上是关于MySQL还不支持限制&& in / all?的主要内容,如果未能解决你的问题,请参考以下文章

This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'

This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'

MySQL分区表的局限和限制

MySQL 5.5 & 5.7 源码包如何升级到MySQL 5.7.26

MySQL (# of Products in Category) COUNT() w/ LEFT JOIN & ON 2 表

python开发mysql:mysql数据类型&约束条件