SQLite, MySQL, PHP: Ternary operator (IF() statement) in MySQL and SQLite SQLite的,MySQL和PHP:三元运算符(中频()语句)在MySQL和SQLite
Posted on 03.发布03。 Feb, 2010 by Dragos in Coding , MySQL , PHP , SQLite 2008年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).而在代理检查工具的工作为我的项目之一,我很难就如何执行我的SQLite数据库有条件的查询来更新列仅当它的值大于0(这将是对我来说,毫无意义,让脚本更新负值的列)。
In Mysql one would do like this (we use the ternary operator IF(to_check_expression>0,expression1,expression2) ):在MySQL的人会不喜欢这样(我们使用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.然而,在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:相关岗位:












































