2012-02-15 73 views
2

我有疑问,在进入查询......需要帮助的访问查询

请告知是否有可能

我已经联系了Excel文件导入访问,其具有的某些列数..My疑问句是

为EG

为了取回笔记本电脑的说明书和地区栏,桌面表

我使用下面的查询

SELECT Laptop.[Description], Laptop.[Region] From Laptop 
union SELECT Desktop.[Description], Desktop.[Region] From Desktop 

有时..它可能不包含地区字段,在那个时候我使用“”作为笔记本电脑。[区域]或“”为桌面。[区域]

我的追求是

有没有这样的

SELECT Laptop.[Description], If Laptop.[Region]=avairable 
    then Laptop.[Region] else “” as [Region] from Laptop; 

或任何方式错误跳过任何选项...

请帮我在这... Thx提前

的疑问:

要清楚

如果桌面表中有说明和区域列..

 
Description Region 
Saran  east 
Sathish  north 
sathy  west

而且

笔记本电脑具有台式表中有描述和价格...

 
Description Cost 
asdf   23 
dkasfjasd 34 
flkasdf  55
Select Laptop.[Description], NZ(Laptop.[Region], "NA") as [Region] 
from Laptop 
UNION 
SELECT Desktop.[Description], NZ(Desktop.[Region], "NA") as [Region] 
FROM Desktop; 

它会返回此结果吗?

,因为我有一些访问问题

 
Description Region 
asdf    
dkasfjasd 
flkasdf 
Saran  east 
Sathish  north 
sathy  west
+0

是纠正它,必须从桌面..我已经纠正了.. THX – 2012-02-16 05:12:23

回答

1

你可以在这个查询中使用switch case,但是在mS-acess中它不被支持,但另一种方法是在访问时使用iif()在这里我给你一个通用的例子,你可以很容易地将它转换成您的实际查询。

IIf(expr, truepart, falsepart) 

SELECT IIF(IsNull(Laptop.[Region])," ",Laptop.[Region]) as region 
FROM Laptop ; 
+0

访问SQL确实有一个'SWITCH()'函数,例如'SELECT SWITCH([区域]不是NULL,[区域],TRUE,'{{无区域}}')从笔记本电脑;' – onedaywhen 2012-02-16 08:14:03

+0

感谢更新我,真的非常感谢 – 2012-02-16 09:10:04

3

我假设你在你的伪代码,“= avairable”是指一个值存在彪我无法运行此。你只是想处理一个空值。

Select Laptop.Description, NZ(Laptop.Region, "") as [Region] from Laptop; 

NZ() function将处理空值并替换任何你想要的。

+2

+1您也可以使用IIF语句,如果你喜欢它 – 2012-02-15 09:51:57

+0

@MattDonnan - 好点。特别是如果你想检查非空值或需要更复杂的东西。 – JeffO 2012-02-15 10:12:30

+0

可以澄清我的任务..我在我的问题中添加了这个问题(疑问:) – 2012-02-16 05:45:19