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 MISES A JOUR VIA RSS | Email Get updates via feedburner Get updates via twitter
Home / Coding / MySQL / PHP / SQLite / SQLite, MySQL, PHP: Ternary … Accueil / Codage / MySQL / PHP / SQLite / SQLite, MySQL, PHP: ternaire ...

SQLite, MySQL, PHP: Ternary operator (IF() statement) in MySQL and SQLite SQLite, MySQL, PHP: opérateur ternaire (if ()) dans MySQL et SQLite

Posted on 03. Posté le 03. Feb, 2010 by Dragos in Coding , MySQL , PHP , SQLite Feb, 2010 par Dragos en codage, 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). Tout en travaillant sur un outil de vérification représente l'un de mes projets, je me débattais sur la façon d'exécuter une requête conditionnelle sur ma base de données SQLite pour mettre à jour une colonne seulement si sa valeur est supérieure à 0 (il serait inutile dans mon cas, de laisser le script de mise à jour la colonne avec des valeurs négatives).

In Mysql one would do like this (we use the ternary operator IF(to_check_expression>0,expression1,expression2) ): En Mysql on le ferait comme cela (nous utilisons l'opérateur ternaire 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. Toutefois, dans SQLite cette méthode ne fonctionnera pas. Instead I had to use the case when (condition) then expression1 else expression2 end method. Au lieu de cela j'ai dû utiliser le cas lorsque (condition) then expression1 else expression2 fin méthode.

The above query in MySQL can be rewritten like this in SQLite: La requête ci-dessus dans MySQL peut être réécrit comme ceci dans 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 someone!

Translate this post Translate this post


Related posts: Related posts:

  1. Wordpress: Easily Reset Your Account Password Using MySQL and PHP Wordpress: Reset Facilement passe de votre compte avec MySQL et PHP
  2. PHP Error: Call to a member function fetch_assoc() on a non-object in PHP Error: Appel à une fonction membre fetch_assoc () sur un objet non dans
  3. PHP: Script to extract one's contacts from email (Gmail, Yahoo,Hotmail,AOL…) and send invites – OpenInviter to go! PHP: Script pour extraire un de ses contacts e-mail (Gmail, Yahoo, Hotmail, AOL ...) et envoyer des invitations - OpenInviter to go!

    blog comments powered by Disqus Commentaires Blog powered by Disqus