SQLite:类似于 Oracle Decode 或 MS“If”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10490254/
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
SQLite: Something like Oracle Decode or MS "If"
提问by Paulb
In the past, I've used "decode" in Oracle, and "iif" in Microsoft, in the SELECT stmt of an SQL query.
过去,我在 SQL 查询的 SELECT stmt 中使用了 Oracle 中的“解码”和 Microsoft 中的“iif”。
I've been considering SQLite for a project, but as far as I can tell, conditionals in the SELECT stmt are difficult. Googling brings up the use of CASE.. but intuitively, that seems that the performance would not be very good, not to mention more difficult to code. Or maybe I don't fully understand it.
我一直在考虑将 SQLite 用于一个项目,但据我所知,SELECT stmt 中的条件很困难。Googling 提到了 CASE 的用法。。但是直觉上,那似乎性能不会很好,更不用说更难编码了。或者我可能没有完全理解它。
Thoughts or advice from anyone? I'd really like to use SQLite for this project.
任何人的想法或建议?我真的很想在这个项目中使用 SQLite。
回答by xQbert
I think case is just as easy to use. It's familiarity with syntax that may be buggin you but this works just as well.
我认为 case 也同样易于使用。熟悉的语法可能会让您感到烦恼,但这也同样有效。
CASE Gender WHEN 'F' then 'FEMALE'
WHEN 'M' then 'MALE'
ELSE 'UNDEFINED'
END as GenderName
or
或者
CASE WHEN GENDER = 'F' THEN 'FEMALE'
WHEN GENDER = 'M' THEN 'MALE'
ELSE 'UNDEFINED'
END as GenderName
It should be noted that CASE is pretty much db agnostic: Decode, iif, if, and other variations are not. So if your query needs to work on several different databases; stick to case as it's more likely to work across multiple database platforms.
应该注意的是,CASE 几乎与数据库无关:Decode、iif、if 和其他变体不是。因此,如果您的查询需要在多个不同的数据库上运行;坚持案例,因为它更有可能跨多个数据库平台工作。