SQL sqlite 是否支持选择中的任何类型的 IF(condition) 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1294619/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 03:15:02  来源:igfitidea点击:

Does sqlite support any kind of IF(condition) statement in a select

sqlsqlite

提问by Matt Peters

Does sqlite support the sql function "if" in the select statement?

sqlite是否支持select语句中的sql函数“if”?

for example

例如

select if( length( a ) > 4 , a , ' ') as b
from foo

which would return a if the length was over 4 chars long. or else it would return ' ' as b

如果长度超过 4 个字符,它将返回 a。否则它会返回 ' ' 作为 b

If it does support a condition in the select what is the syntax is should be using?

如果它确实支持 select 中的条件,则应该使用什么语法?

I have checked http://sqlite.org/lang_corefunc.htmlbut I can't see it.

我已经检查过 http://sqlite.org/lang_corefunc.html但我看不到它。

回答by user151019

See the caseexpression.

参见案例表达。

A CASE expression serves a role similar to IF-THEN-ELSE in other programming languages.

CASE 表达式的作用类似于其他编程语言中的 IF-THEN-ELSE。

For your example

对于你的例子

select case when length(a) > 4 then a else '' end as b
from foo

回答by Paul Dixon

You can use casefor that:

您可以为此使用案例

select case when length(a)>4 then a else ' ' end from foo;