Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite IF Exists Clause

Tags:

sql

sqlite

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?

if exists (select username from tbl_stats_assigned where username = 'abc' )
    select 1 as uname
else
    select 0 as uname
like image 445
thinkster Avatar asked Oct 12 '11 20:10

thinkster


1 Answers

Just do it the standard SQL way:

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).

That should work in any SQL database and some even have special optimizations to support that idiom.

like image 113
mu is too short Avatar answered Sep 30 '22 16:09

mu is too short