This is a translated page. The original can be found here: http://iwebdevel.com/2010/02/03/sqlite-mysql-php-ternary-operator-if-statement-in-mysql-and-sqlite/
UPDATES VIA RSS | Email Actualizacións Via RSS | Email Get updates via feedburner Get updates via twitter
Home / Coding / MySQL / PHP / SQLite / SQLite, MySQL, PHP: Ternary … Home / Codificación / MySQL / PHP / SQLite / SQLite, MySQL, PHP: ternários ...

SQLite, MySQL, PHP: Ternary operator (IF() statement) in MySQL and SQLite SQLite, MySQL, PHP: operador ternário (IF () declaración) no MySQL e SQLite

Posted on 03. Posta en 03. Feb, 2010 by Dragos in Coding , MySQL , PHP , SQLite Febreiro de 2010 por Dragos na Codificación, 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). Mentres traballaba nun instrumento de procuração comprobar a un dos meus proxectos, eu estaba loitando sobre como realizar unha consulta condicional no meu base de datos SQLite para actualizar unha columna só se o seu valor sexa superior a 0 (que sería inútil no meu caso, para deixar o script de actualización a columna con valores negativos).

In Mysql one would do like this (we use the ternary operator IF(to_check_expression>0,expression1,expression2) ): En MySQL un faría coma este (que utilizamos o comando ternário IF (to_check_expression> 0, expressão1, expressão2)):

update table_name set column_name=IF((column_name-1)>0,(column_name-1),0)

However, in SQLite this method will not work. Sen embargo, en SQLite, este método non funcionará. Instead I had to use the case when (condition) then expression1 else expression2 end method. En canto eu tiven que usar o caso cando (condición) entón o método de expressão1 máis expression2 final.

The above query in MySQL can be rewritten like this in SQLite: A consulta anterior no MySQL pode ser reescrita coma este no 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! Espero que axude a alguén!

Translate this post Traducir esta post


Related posts: Related posts:

  1. Wordpress: Easily Reset Your Account Password Using MySQL and PHP Wordpress: facilmente axustar o teu contrasinal da conta utilizando MySQL e PHP
  2. PHP Error: Call to a member function fetch_assoc() on a non-object in PHP Error: Call to a member function fetch_assoc () sobre un obxecto non-in

    blog comments powered by Disqus blog comments powered by Disqus