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 Aktualisierungen per RSS | E-Mail Get updates via feedburner Get updates via twitter
Home / Coding / MySQL / PHP / SQLite / SQLite, MySQL, PHP: Ternary … Home / Coding / MySQL / PHP / SQLite / SQLite, MySQL, PHP: Ternary ...

SQLite, MySQL, PHP: Ternary operator (IF() statement) in MySQL and SQLite SQLite, MySQL, PHP: Ternary-Operator (IF ()-Anweisung) in MySQL und SQLite

Posted on 03. Gesendet am 03. Feb, 2010 by Dragos in Coding , MySQL , PHP , SQLite Februar, 2010 von Dragos in 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). Während der Arbeit auf einem Proxy-Checker Tool für eines meiner Projekte war ich zu kämpfen, wie eine bedingte Abfrage auf meine SQLite-Datenbank ausführen, um eine Spalte nur, wenn der Wert größer als 0 (in meinem Fall wäre es sinnlos, die wir zu aktualisieren Skript aktualisiert die Spalte mit negativen Werten).

In Mysql one would do like this (we use the ternary operator IF(to_check_expression>0,expression1,expression2) ): In Mysql das würde doch davon (wir verwenden der ternäre Operator 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. Allerdings wird in SQLite diese Methode nicht funktionieren. Instead I had to use the case when (condition) then expression1 else expression2 end method. Stattdessen musste ich auf den Fall zu verwenden, wenn (Bedingung), dann expression1 sonst expression2 Ende Methode.

The above query in MySQL can be rewritten like this in SQLite: Die obige Abfrage in MySQL kann wie folgt in SQLite umgeschrieben werden:

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: In Verbindung stehende Pfosten:

  1. Wordpress: Easily Reset Your Account Password Using MySQL and PHP Wordpress: Leicht Reset "Mein Konto" Passwort mit MySQL und PHP
  2. PHP Error: Call to a member function fetch_assoc() on a non-object in PHP Error: Aufruf einer Member-Funktion fetch_assoc () auf einem Nicht-Objekt in
  3. PHP: Script to extract one's contacts from email (Gmail, Yahoo,Hotmail,AOL…) and send invites – OpenInviter to go! PHP: ein Script, um die Kontakte aus E-Mail (Gmail, Yahoo, Hotmail, AOL ...) zu extrahieren und Einladungen versenden - OpenInviter to go!

    blog comments powered by Disqus Blog-Kommentare powered by Disqus