我在MSAccess中创建了三个表(PCH Info,Attorney zip-county,Medical-zip code-county)。每个表都有类似的邮政编码,城市和县的字段。我的目标是在3表中搜索邮政编码,城市和县中的任何一个,并返回满足一个或两个搜索条件的结果。 我已经创建了3个搜索字段,并在三个表上使用UNION构建我的查询。但我的问题是,如果我专门搜索数据,它会返回结果,其他结果不符合标准。见下面的代码
SELECT [PCH Info].City, [PCH Info].[Zip Code], [PCH Info].County, "PCH Info"
FROM [PCH Info]
WHERE (([PCH Info].City) Like Forms!Search.qcity & "*") And (([PCH Info].[Zip Code]) Like Forms!Search.qcode & "*") And (([PCH Info].County) Like Forms!Search.qcounty & "*") OR (([PCH Info].[Network Member]) = Yes)
UNION
SELECT [Medical-Zip Code-County].City, [Medical-Zip Code-County].[ZIP Code], [Medical-Zip Code-County].County , "Medical-Zip Code-County"
FROM [Medical-Zip Code-County]
WHERE (([Medical-Zip Code-County].City) Like Forms!Search.qcity & "*") And (([Medical-Zip Code-County].[ZIP Code]) Like Forms!Search.qcode & "*") And (([Medical-Zip Code-County].County) Like Forms!Search.qcounty & "*")
UNION
SELECT [Attorney-Zip-County].City, [Attorney-Zip-County].[ZIP Code], [Attorney-Zip-County].[Country/Region], "Attorney-Zip-County"
FROM [Attorney-Zip-County]
WHERE (([Attorney-Zip-County].City) Like Forms!Search.qcity & "*") And (([Attorney-Zip-County].[ZIP Code]) Like Forms!Search.qcode & "*") And (([Attorney-Zip-County].[Country/Region]) Like Forms!Search.qcounty & "*");
我的主要挑战来自第一个查询,其中PCH Info.Network会员=是,如果我进行搜索以满足此标准,它仍然给我符合标准的结果。我可能做错了什么或UNION查询问题?谢谢。
答案 0 :(得分:0)
怎么样:
SELECT City, [ZIP Code], County, Source, [Network Member]
FROM (
SELECT p.City, p.[Zip Code], p.County,
"PCH Info" As Source, p.[Network Member]
FROM [PCH Info] p
UNION ALL
SELECT m.City, m.[ZIP Code], m.County ,
"Medical-Zip Code-County" As Source, False As [Network Member]
FROM [Medical-Zip Code-County] m
UNION ALL
SELECT a.City, a.[ZIP Code], a.[Country/Region],
"Attorney-Zip-County" As Source, False As [Network Member]
FROM [Attorney-Zip-County] a) As r
WHERE (r.City Like Forms!Search!qcity & "*"
And r.[Zip Code] Like Forms!Search!qcode & "*"
And r.County Like Forms!Search!qcounty & "*")
OR r.[Network Member] = Yes