SQLite, MySQL, PHP: Ternary operator (IF() statement) in MySQL and SQLite SQLite, MySQL, PHP: Ternary operator (IF () pernyataan) di MySQL dan SQLite
Posted on 03. Posted on 03. Feb, 2010 by Dragos in Coding , MySQL , PHP , SQLite Februari, 2010 oleh Dragos di Coding, MySQL, PHP, SQLite
While working on a proxy checker tool for one of my projects, I was struggling on how to execute a conditional query on my SQLite database to update a column only if its value is greater than 0 (it would be pointless in my case to let the script update the column with negative values). Ketika bekerja pada alat checker proxy untuk salah satu proyek saya, saya sedang berjuang tentang cara untuk mengeksekusi kueri bersyarat database SQLite saya untuk memperbarui kolom hanya jika nilainya lebih besar dari 0 (itu akan sia-sia dalam kasus saya untuk membiarkan script update kolom dengan nilai negatif).
In Mysql one would do like this (we use the ternary operator IF(to_check_expression>0,expression1,expression2) ): Dalam Mysql satu akan melakukan seperti ini (kami menggunakan operator terner IF (to_check_expression> 0, expression1, expression2)):
update table_name set column_name=IF((column_name-1)>0,(column_name-1),0)
However, in SQLite this method will not work. Namun, dalam SQLite metode ini tidak akan bekerja. Instead I had to use the case when (condition) then expression1 else expression2 end method. Sebaliknya aku harus menggunakan kasus ketika (kondisi) maka expression1 lain expression2 metode akhir.
The above query in MySQL can be rewritten like this in SQLite: Query di atas di MySQL bisa ditulis seperti ini di SQLite:
update table_name set column_name=case when (column_name-1)>0 then (column_name-1) else 0 end
That's it. That's it. I hope it helps someone! I hope it helps seseorang!
Related posts: Related posts:












































