试试看
SELECT * FROM WorkOrders WHERE WorkOrderID IN (
SELECT WorkOrderID FROM Jobs WHERE jobID IN (
SELECT jobID FROM Jobs_Status WHERE StatusID =9))
我想清除的一件事是jobID (1,2,5,6,7)
,StatusID =9
所以WorkOrderID
反对jobID (1,2,5,6,7)
是6,7所以你说的依据是什么
根据表格中给出的数据,我们将只有一个结果行,即 WorkOrderID 6,因为 WorkOrderID==6 下的所有作业(JobID ==1 和 2)具有 StatusID ==9
它将返回两个结果WorkOrderID 6,7
现在这将WorkOrderID 6
根据您的要求返回
SELECT q.* FROM (
SELECT w.`WorkOrderID`,
(SELECT
GROUP_CONCAT(`jobID` SEPARATOR ',') FROM `jobs` WHERE `jobs`.`WorkOrderID`=w.`WorkOrderID` GROUP BY `jobs`.`WorkOrderID`) AS Alljobids
, GROUP_CONCAT(j.`jobID` SEPARATOR ',') AS onlystatusids
FROM `workorders` w INNER JOIN `jobs` j ON (w.`WorkOrderID` = j.`WorkOrderID`)
INNER JOIN `jobs_status` js ON (j.`jobID` = js.`jobID`) WHERE js.`StatusID`=9
GROUP BY w.`WorkOrderID`) q WHERE q.Alljobids=q.onlystatusids
这是另一种使用方式HAVING
SELECT w.`WorkOrderID`,
(SELECT
GROUP_CONCAT(`jobID` SEPARATOR ',') FROM `jobs` WHERE `jobs`.`WorkOrderID`=w.`WorkOrderID` GROUP BY `jobs`.`WorkOrderID`) AS Alljobids
, GROUP_CONCAT(j.`jobID` SEPARATOR ',') AS onlystatusids
FROM `workorders` w INNER JOIN `jobs` j ON (w.`WorkOrderID` = j.`WorkOrderID`)
INNER JOIN `jobs_status` js ON (j.`jobID` = js.`jobID`) WHERE js.`StatusID`=9
GROUP BY w.`WorkOrderID`
HAVING Alljobids=onlystatusids
这是你的小提琴例子如果你不相信:)