访问SQL查询IIF语句太复杂

时间:2016-06-21 20:22:48

标签: sql ms-access

我正在使用Access数据库并使用IFF编写查询以输出缩写状态,如此

IIf(Trim(CtblStates.StateName) = 'New York', 'NY', 
IIf(Trim(CtblStates.StateName) = 'Maryland', 'MD', 
IIf(Trim(CtblStates.StateName) = 'Virginia', 'VA', 
IIf(Trim(CtblStates.StateName) = 'New Jersey', 'NJ', 
IIf(Trim(CtblStates.StateName) = 'District of Columbia', 'DC', 
IIf(Trim(CtblStates.StateName) = 'Florida', 'FL', 
IIf(Trim(CtblStates.StateName) = 'West Virginia', 'WV', 
IIf(Trim(CtblStates.StateName) = 'Georgia', 'GA', 
IIf(Trim(CtblStates.StateName) = 'Alabama', 'AL', 
IIf(Trim(CtblStates.StateName) = 'Alaska', 'AK', 
IIf(Trim(CtblStates.StateName) = 'California', 'CA', 
IIf(Trim(CtblStates.StateName) = 'Pennsylvania', 'PA',
IIf(Trim(CtblStates.StateName) = 'Delaware', 'DE',
CtblStates.StateName  )))))))))))))   AS State, 

当我尝试添加更多状态时,我得到了'IFF语句过于复杂'。 我知道你只能添加这么多IIF语句,但我需要处理 50个州中的每一个。我怎么能绕过这个?

由于

3 个答案:

答案 0 :(得分:4)

为了帮助使@ JohnCappelletti的建议更容易,SQL添加新表如下所示。然后加入你的查询。

CREATE TABLE states (stateName CHAR, stateAbbr CHAR);
INSERT INTO states (stateName, stateAbbr) VALUES ('Alabama','AL');
INSERT INTO states (stateName, stateAbbr) VALUES ('Alaska','AK');
INSERT INTO states (stateName, stateAbbr) VALUES ('Arizona','AZ');
INSERT INTO states (stateName, stateAbbr) VALUES ('Arkansas','AR');
INSERT INTO states (stateName, stateAbbr) VALUES ('California','CA');
INSERT INTO states (stateName, stateAbbr) VALUES ('Colorado','CO');
INSERT INTO states (stateName, stateAbbr) VALUES ('Connecticut','CT');
INSERT INTO states (stateName, stateAbbr) VALUES ('Delaware','DE');
INSERT INTO states (stateName, stateAbbr) VALUES ('Florida','FL');
INSERT INTO states (stateName, stateAbbr) VALUES ('Georgia','GA');
INSERT INTO states (stateName, stateAbbr) VALUES ('Hawaii','HI');
INSERT INTO states (stateName, stateAbbr) VALUES ('Idaho','ID');
INSERT INTO states (stateName, stateAbbr) VALUES ('Illinois','IL');
INSERT INTO states (stateName, stateAbbr) VALUES ('Indiana','IN');
INSERT INTO states (stateName, stateAbbr) VALUES ('Iowa','IA');
INSERT INTO states (stateName, stateAbbr) VALUES ('Kansas','KS');
INSERT INTO states (stateName, stateAbbr) VALUES ('Kentucky','KY');
INSERT INTO states (stateName, stateAbbr) VALUES ('Louisiana','LA');
INSERT INTO states (stateName, stateAbbr) VALUES ('Maine','ME');
INSERT INTO states (stateName, stateAbbr) VALUES ('Maryland','MD');
INSERT INTO states (stateName, stateAbbr) VALUES ('Massachusetts','MA');
INSERT INTO states (stateName, stateAbbr) VALUES ('Michigan','MI');
INSERT INTO states (stateName, stateAbbr) VALUES ('Minnesota','MN');
INSERT INTO states (stateName, stateAbbr) VALUES ('Mississippi','MS');
INSERT INTO states (stateName, stateAbbr) VALUES ('Missouri','MO');
INSERT INTO states (stateName, stateAbbr) VALUES ('Montana','MT');
INSERT INTO states (stateName, stateAbbr) VALUES ('Nebraska','NE');
INSERT INTO states (stateName, stateAbbr) VALUES ('Nevada','NV');
INSERT INTO states (stateName, stateAbbr) VALUES ('New Hampshire','NH');
INSERT INTO states (stateName, stateAbbr) VALUES ('New Jersey','NJ');
INSERT INTO states (stateName, stateAbbr) VALUES ('New Mexico','NM');
INSERT INTO states (stateName, stateAbbr) VALUES ('New York','NY');
INSERT INTO states (stateName, stateAbbr) VALUES ('North Carolina','NC');
INSERT INTO states (stateName, stateAbbr) VALUES ('North Dakota','ND');
INSERT INTO states (stateName, stateAbbr) VALUES ('Ohio','OH');
INSERT INTO states (stateName, stateAbbr) VALUES ('Oklahoma','OK');
INSERT INTO states (stateName, stateAbbr) VALUES ('Oregon','OR');
INSERT INTO states (stateName, stateAbbr) VALUES ('Pennsylvania','PA');
INSERT INTO states (stateName, stateAbbr) VALUES ('Rhode Island','RI');
INSERT INTO states (stateName, stateAbbr) VALUES ('South Carolina','SC');
INSERT INTO states (stateName, stateAbbr) VALUES ('South Dakota','SD');
INSERT INTO states (stateName, stateAbbr) VALUES ('Tennessee','TN');
INSERT INTO states (stateName, stateAbbr) VALUES ('Texas','TX');
INSERT INTO states (stateName, stateAbbr) VALUES ('Utah','UT');
INSERT INTO states (stateName, stateAbbr) VALUES ('Vermont','VT');
INSERT INTO states (stateName, stateAbbr) VALUES ('Virginia','VA');
INSERT INTO states (stateName, stateAbbr) VALUES ('Washington','WA');
INSERT INTO states (stateName, stateAbbr) VALUES ('West Virginia','WV');
INSERT INTO states (stateName, stateAbbr) VALUES ('Wisconsin','WI');
INSERT INTO states (stateName, stateAbbr) VALUES ('Wyoming','WY');

答案 1 :(得分:1)

不要那样做!

使用“字典表”。将新表添加到MS Access数据库。定义2列。一个 - “FullName”和第二个 - “ShortName”。然后添加相应的值:

"New York" | "NY"
"Maryland" | "MD"

现在,使用join

SELECT a.*, b.ShortName
FROM YourTable As a INNER JOIN DictionaryTable AS b ON a.StateName = b.FullName;

随意根据您的需求改进查询。

答案 2 :(得分:0)

因为您的表名称我认为您需要在表CtblStates上创建新字段:

  1. 向名为[CtblStates]的表State添加新字段,并根据每行填写状态数据。
  2. 将您的查询替换为[CtblStates].State