Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional update in SQLite

Tags:

sql

sqlite

Can I create in SQLite a statement like this?

update books set
(if number_of_pages > 0 then number_of_pages = number_of_pages - 1)
where book_id = 10

Is there a working syntax for this?

like image 268
xralf Avatar asked Feb 26 '12 13:02

xralf


2 Answers

A CASE statement should work with the following syntax:

UPDATE
  books
SET number_of_page = CASE WHEN number_of_pages > 0 THEN (number_of_pages - 1) ELSE 0 END
WHERE whatever_condition
like image 123
Michael Berkowski Avatar answered Oct 05 '22 01:10

Michael Berkowski


Isnt that equal to this statement ?

update books set number_of_pages = number_of_pages - 1 where number_of_pages>0

Edit:

according to new statement :

update books set number_of_pages = number_of_pages - 1 where number_of_pages>0 and book_id = 10
like image 29
Pheonix Avatar answered Oct 05 '22 02:10

Pheonix