我正在尝试为Microsoft Access编写以下查询,并且在这里遇到了最后三个字符的问题,有人可以告诉我出于访问目的的语法有什么问题:
SELECT DISTINCT Searches.Search,IIF(Chemical_Names.Category IS NOT NULL,Chemical_Names.Category,IIF(CAS_Numbers.Category IS NOT NULL,CAS_Numbers.Category,IIF(Brand_Name.Category IS NOT NULL,Brand_Name.Category,
IIF(PART.Category IS NOT NULL,PART.Category,CAT.Category))))AS类别
来自搜索
LEFT JOIN
(SELECT * FROM Cat1 UNION
SELECT * FROM Cat2 UNION
SELECT * FROM Cat3 UNION
选择*来自Cat4)作为CAT
上
Searches.Search = CAT.Search
LEFT JOIN
(SELECT * FROM Part1 UNION
SELECT * FROM Part2 UNION
SELECT * FROM Part3 UNION
SELECT * FROM Part4)作为PART
上
Searches.Search = PART.Search
LEFT JOIN
供应商名称
上
Searches.Search = Supplier_Name.Search
LEFT JOIN
Chemical_Names
上
Searches.Search = Chemical_Names.Search
LEFT JOIN
Chemical_Names
上
Searches.Search = CAS_Number.Search
LEFT JOIN
品牌
上
Searches.Search = Brand_Name.Search;
答案 0 :(得分:2)
我认为您可以尝试使用Nz()
函数有条件地在您的首选项链中显示第一个非null类别:
SELECT DISTINCT
Nz(Chemical_Names.Category,
Nz(CAS_Number.Category,
Nz(Brand_Name.Category,
Nz(PART.Category, CAT.Category)))) AS Category
FROM Searches
...
Nz()
函数是大多数其他数据库中COALESCE()
的“Access”版本。
您还可以处理此使用IIF()
,这是CASE
表达式的Access'版本:
SELECT DISTINCT
IIF(Chemical_Names.Category IS NOT NULL, Chemical_Names.Category,
IIF(CAS_Number.Category IS NOT NULL, CAS_Number.Category,
IIF(Brand_Name.Category IS NOT NULL, Brand_Name.Category,
IIF(PART.Category IS NOT NULL, PART.Category, CAT.Category)))) AS Category
FROM Searches
...
答案 1 :(得分:1)
如果我理解您想要的结果,应按如下方式应用Switch:
Switch(Chemical_Names.Category IS NOT NULL, Chemical_Names.Category, CAS_Namber.Category IS NOT NULL, CAS_Number.Category, Brand_Name.Category IS NOT NULL, Brand_Name.Category, PART.Category IS NOT NULL, PART.Category, 1=1, CAT.Category) AS Category