Microsoft Access Query Syntax Access (using IN clause within an IN clause)

戏子无情 提交于 2020-01-07 08:24:04

问题


Hi I have had a problem with the following query. I am using two IN clauses to find the type=LEFT for only type=JOIN has occurred once. However, the code is breaking.

Ultimately, I am trying to find how many animals left the Zoo without ever JOIN the cage. Thus, it outdates the database knowledge as I have added animals leaving without the join date

I believe the following code should be the resolution however it is not working To my understanding I need an IN clause within an IN clause

SELECT animal8001.animalname, join8001.type, join8001.joindate
FROM carer8001 INNER JOIN ((cage8001 INNER JOIN (animal8001 INNER JOIN join8001 ON animal8001.animalid = join8001.animalid) ON cage8001.cageid = join8001.cageid) INNER JOIN care8001 ON cage8001.cageid = care8001.cageid) ON carer8001.carerid = care8001.carerid
WHERE (((join8001.type) In (SELECT join8001.type 
                                FROM   animal8001 
                                       INNER JOIN join8001 
                                               ON animal8001.animalid = 
                                                  join8001.animalid 
                                GROUP  BY join8001.type, 
                                          animal8001.animalid 
                                HAVING ( ( ( join8001.type ) = "join" ) 
                                         AND 
        ( ( Count(join8001.type) ) = 0 ) ))))
GROUP BY animal8001.animalname, join8001.type, join8001.joindate
HAVING (((join8001.type)="LEFT"));


回答1:


If you only want to know which animals have a LEFT record with no JOIN records ever, simply:

SELECT DISTINCT AnimalCages.AnimalID
FROM AnimalCages
WHERE (((AnimalCages.AnimalID) Not In (SELECT AnimalID FROM AnimalCages WHERE Type="JOIN")));

If you want animals which have no JOIN records at all as well as those which have JOIN but not following a LEFT, consider:

SELECT AnimalCages.JoinDate, AnimalCages.AnimalID, AnimalCages.Type
FROM (SELECT Max(AnimalCages.JoinDate) AS MaxOfJoinDate, AnimalCages.AnimalID
      FROM AnimalCages
      WHERE (((AnimalCages.Type)="JOIN"))
      GROUP BY AnimalCages.AnimalID) AS MaxJOIN 
RIGHT JOIN AnimalCages ON MaxJOIN.AnimalID = AnimalCages.AnimalID
WHERE (((AnimalCages.AnimalID) Not In (SELECT AnimalID FROM AnimalCages 
           WHERE Type="JOIN"))) OR (((AnimalCages.JoinDate)>[MaxOfJoinDate]));


来源:https://stackoverflow.com/questions/58652601/microsoft-access-query-syntax-access-using-in-clause-within-an-in-clause

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!