SQLite, MySQL, PHP: Ternary operator (IF() statement) in MySQL and SQLite SQLiteは、MySQL、PHPの:3項演算子(MySQLとSQLite()文)は、IF
Posted on 03. 03に掲載した。 Feb, 2010 by Dragos in Coding , MySQL , PHP , SQLite 2月、2010年符号化 、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).中には、プロキシチェッカーツールの1つ私のプロジェクトのための作業、私はいかに私のSQLiteデータベース上の列の場合にのみ、その値が0より大きい(それは私のケースでは、letは無意味になる更新するために条件付きのクエリを実行する上で苦しんでいたスクリプト)が負の値を持つ列を更新します。
In Mysql one would do like this (we use the ternary operator IF(to_check_expression>0,expression1,expression2) ): mysqlの1つでは、この(私たち(to_check_expression"0式expression1、expression2の))IFの三項演算子を使用するようにしないと:
update table_name set column_name=IF((column_name-1)>0,(column_name-1),0)
However, in SQLite this method will not work.ただし、SQLiteは、このメソッドでは動作しません。 Instead I had to use the case when (condition) then expression1 else expression2 end method.その代わり、私の場合を使用していたとき(条件)をexpression1の他のexpression2の終了方法。
The above query in MySQL can be rewritten like this in SQLite: MySQLでは、上記のクエリはこのようなSQLiteで書き換えることができます:
update table_name set column_name=case when (column_name-1)>0 then (column_name-1) else 0 end
That's it.それだけだ。 I hope it helps someone!私はそれを誰か支援を期待!
Related posts:関連記事:












































