SQL SQLite IF Exists 子句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7746306/
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 IF Exists Clause
提问by thinkster
How to write IF EXISTS as shown in below query in SQLite? I read somewhere that IF clause doesn't exist in SQLite. What would be a better alternative for this?
如何在SQLite中编写如下查询所示的IF EXISTS?我在某处读到 SQLite 中不存在 IF 子句。对此有什么更好的选择?
if exists (select username from tbl_stats_assigned where username = 'abc' )
select 1 as uname
else
select 0 as uname
回答by mu is too short
Just do it the standard SQL way:
只需按照标准的 SQL 方式进行操作:
select exists(
select 1
from tbl_stats_assigned
where username = 'abc'
);
Assuming of course that your 1
and 0
are actually boolean values (which SQLite represents with one and zero just like MySQL).
当然,假设您的1
和0
实际上是布尔值(SQLite 用 1 和 0 表示,就像 MySQL 一样)。
That should work in any SQL database and some even have special optimizations to support that idiom.
这应该适用于任何 SQL 数据库,有些甚至有特殊的优化来支持该习惯用法。