简体   繁体   English

IIF语句MS ACCESS VBA SQL

[英]IIF statement MS ACCESS VBA SQL

I have 3 tables 我有3张桌子

ConnectivityFile where I have the ids for some properties 我有一些属性ID的ConnectivityFile

CHAINDD where I have the hotel codes (they can be repeated) for the same properties CHAINDD ,其中我有相同属性的酒店代码(可以重复)

Export where I want to have all the hotel codes and their ids (a join for the previous tables) Export我想要的所有酒店代码及其ID的位置(以前表格的联接)

I can achieve that with the query below 我可以通过下面的查询来实现

query2 = "SELECT ConnectivityFile.ID as [Hotel ID], ConnectivityFile.HotelCode  
FROM ConnectivityFile 
INNER JOIN (SELECT DISTINCT CHAINDD.HOTELCODE as [Hotel Code] from CHAINDD) as T 
ON ConnectivityFile.HotelCode = T.[Hotel Code]"

DoCmd.RunSQL "INSERT INTO Export " & query2

Now the issue I am facing is that let's say the hotel code from table CHAINDD is not present on table ConnectivityFile. 现在我面临的问题是,假设表CHAINDD中的酒店代码不存在于表ConnectivityFile中。 I would like to point that out as "Not Connected" as the ID on the export table. 我想指出的是导出表上的ID为“未连接”。

How can achieve that? 如何实现呢? I know there is the IIF statement when I have done some trial and error with no success. 我知道经过一些反复试验但没有成功时,会有IIF声明。 Where do you place the IIf statement? 您在哪里放置IIf声明?

Thank you very much for all your help and time 非常感谢您的帮助和时间

as long as your query actually works, the IIF goes something like this: 只要您的查询实际可行,IIF就会像这样:
query2 = "SELECT IIF(ISNULL(T.HOTELCODE), ""Not Connected"", ConnectivityFile.ID) as [Hotel ID], ConnectivityFile.HotelCode query2 =“ SELECT IIF(ISNULL(T.HOTELCODE),”“未连接”“,ConnectivityFile.ID)作为[酒店ID],ConnectivityFile.HotelCode
FROM ConnectivityFile LEFT JOIN (SELECT DISTINCT CHAINDD.HOTELCODE as [Hotel Code] from CHAINDD) as T ON ConnectivityFile.HotelCode = T.[Hotel Code]" 从ConnectivityFile左联接(从CHAINDD中选择DISTINCT CHAINDD.HOTELCODE作为CHAINDD的[酒店代码])作为T ON ConnectivityFile.HotelCode = T. [酒店代码]“

basically, if the code is there, use it, otherwise put a string staying "Not connected" 基本上,如果有代码,请使用它,否则将字符串放在“未连接”
Note that the hotel code field in Export must be able to handle NON-unique data - ie: not be a primary key. 请注意,导出中的酒店代码字段必须能够处理非唯一数据-即:不是主键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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