我在MS Access 2010中工作,能够使用我们存储在专有数据库中的数据运行自己的查询/报告。我将此查询编写为两个子查询,每个子查询都可以完全独立运行。但是,当我一起运行它们时,我得到一个参数输入框,说我需要输入Query4.company_id的参数。我仔细检查了拼写错误,所以情况似乎并非如此。我认为我的子查询连接到主查询的方式一定存在问题。我是一个新手,但很高兴得到你可能有的任何建议!这是我的问题:
SELECT dbo_Companies.company_id,dbo_Companies.company_name,dbo_Lease.lease_from_dt,dbo_Lease.lease_to_dt,dbo_Lease.lease_status,dbo_Lease.lease_description,dbo_Companies.company_status
FROM dbo_Companies LEFT JOIN dbo_Lease ON dbo_Companies.company_id = dbo_Lease.company_id
WHERE(((dbo_Companies.company_id)IN
(SELECT dbo_Companies.company_id
FROM(dbo_Companies INNER JOIN dbo_CompanyFacilities ON dbo_Companies.company_id = dbo_CompanyFacilities.company_id)INNER JOIN dbo_Facilities ON dbo_CompanyFacilities.facility_id = dbo_Facilities.facility_id
WHERE(((dbo_CompanyFacilities.facility_id)= 11或(dbo_CompanyFacilities.facility_id)= 13或(dbo_CompanyFacilities.facility_id)= 14或(dbo_CompanyFacilities.facility_id)= 15或 (dbo_CompanyFacilities.facility_id)= 21或(dbo_CompanyFacilities.facility_id)= 28或(dbo_CompanyFacilities.facility_id)= 24或(dbo_CompanyFacilities.facility_id)= 27或 (dbo_CompanyFacilities.facility_id)= 31或(dbo_CompanyFacilities.facility_id)= 32或(dbo_CompanyFacilities.facility_id)= 33或(dbo_CompanyFacilities.facility_id)= 37) AND(dbo_Companies.company_status =“Active”)AND(((dbo_Companies.company_class_id)= 1)OR((dbo_Companies.company_class_id)= 14))))));
答案 0 :(得分:0)
试试这个:
SELECT
C.company_id,
C.company_name,
L.lease_from_dt,
L.lease_to_dt,
L.lease_status,
L.lease_description,
C.company_status
FROM dbo_Companies C
LEFT JOIN dbo_Lease L
ON C.company_id = L.company_id
WHERE C.company_id IN
(SELECT C2.company_id
FROM dbo_Companies C2
INNER JOIN dbo_CompanyFacilities CF
ON C2.company_id = CF.company_id
INNER JOIN dbo_Facilities F
ON CF.facility_id = F.facility_id
WHERE
CF.facility_id IN (11, 13, 14, 15, 21, 28, 24, 27, 31, 32, 33, 37)
AND C2.company_status = "Active"
AND C2.company_class_id IN (1, 14)
);
要对此进行测试,请先运行第二个 SELECT 语句(parens中的一个)并查看是否有任何结果。如果是这样,请运行整个代码块,看看是否仍然出现该错误。在不了解数据的情况下,它看起来应该运行正常。