SQL:IF 语句 Access 2010
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5349998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQL: IF statement Access 2010
提问by Juronis
I have trying to write a query in Access 2010. I have a table:
我试图在 Access 2010 中编写一个查询。我有一个表:
Table name is power
. I have trying to write IF statement:
表名是power
. 我试图写 IF 语句:
Select IIf(power.gain_type = 'D', power.gain_max + 2.15)
If gain_type equals D, then gain_max sum 2.15
如果gain_type 等于D,则gain_max sum 2.15
For example:
例如:
14.8 + 2.15 = 16.95.
14.8 + 2.15 = 16.95。
Thanks in advance!
提前致谢!
回答by RichardTheKiwi
Now I wondering how to insert an ELSEIF statment. "IF (gain_type='D'){gain_max+2.15} ELSEIF (gain_type='I'){gain_max-2.15} ELSE {gain_max}
现在我想知道如何插入 ELSEIF 语句。"IF (gain_type='D'){gain_max+2.15} ELSEIF (gain_type='I'){gain_max-2.15} ELSE {gain_max}
You can either use SWITCH
您可以使用 SWITCH
Select power.gain_max + Switch(power.gain_type='D', 2.15,
power.gain_type='I', -2.15,
true, 0)
from power
or nest/chain the IIFs
或嵌套/链接 IIF
Select power.gain_max + IIf(power.gain_type='D', 2.15,
IIf(power.gain_type='I', -2.15, 0))
from power
Original
原来的
This does the select
这做选择
Select IIf(power.gain_type='D', power.gain_max+2.15, power.gain_max)
from power
Are you trying to update?
你在尝试更新吗?
update power
set gain_max = gain_max+2.15
where gain_type='D'
You can also use the fact that TRUE = -1 in Access
您还可以在 Access 中使用 TRUE = -1 的事实
Select power.gain_max-2.15*(power.gain_type='D')
from power
References
参考
回答by Andomar
The syntax is iif(condition, value_if_true, value_if_false)
. If you add a third parameter you should be fine:
语法是iif(condition, value_if_true, value_if_false)
. 如果您添加第三个参数,您应该没问题:
IIf(power.gain_type='D',
power.gain_max+2.15,
power.gain_max)
回答by SausageFingers
Result: IIf([gain_type]="D",[gain_max]+2.15,[gain_max])
结果:IIf([gain_type]="D",[gain_max]+2.15,[gain_max])