SQLite IF Exists clause

How to write IF EXISTS as shown below in SQLite? I read somewhere that the IF clause does not exist in SQLite. What would be a better alternative to this?

if exists (select username from tbl_stats_assigned where username = 'abc' ) select 1 as uname else select 0 as uname 
+8
sql sqlite
source share
1 answer

Just do it in the standard SQL way:

 select exists( select 1 from tbl_stats_assigned where username = 'abc' ); 

Assuming your 1 and 0 are actually booleans (which SQLite represents with one and zero, like MySQL).

This should work in any SQL database, and some even have special optimizations to support this idiom.

+16
source share

All Articles