简体   繁体   中英

MS Access Multiple SELECT Query

I have three tables:

Fields

ID | FarmID | FIELD_NUM | Year

Farms

ID | ClientID

Clients

ID | CLIENT_NUM

I have a simple query to get CLIENT_NUM from the Clients table:

SELECT 
  (SELECT CLIENT_NUM FROM Clients 
  WHERE ID = (SELECT ClientID FROM Farms WHERE ID = FarmID)) AS CID
FROM Fields 
WHERE Year = 2014 AND FIELD_NUM = '28-2'

In SQL Server, this works just fine; however, in Microsoft Access it takes forever. Is there a better way to run this query?

Instead of using sub-queries, try using JOIN . Depending on how Access optimizes the query it could help with performance, but also it helps a lot with readability:

SELECT Clients.CLIENT_NUM as CID
FROM (Clients INNER JOIN Farms ON Clients.ID = Farms.ClientID)
    INNER JOIN Fields ON Farms.ID = Fields.FarmID
WHERE Fields.Year = 2014 AND Fields.FIELD_NUM = '28-2'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM