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 Updates via RSS | E-post Get updates via feedburner Get updates via twitter
Home / Coding / MySQL / PHP / SQLite / SQLite, MySQL, PHP: Ternary … Avaleht / kodeerimise / MySQL / PHP / SQLite'i / SQLite'i, MySQL PHP: Ternaarne ...

SQLite, MySQL, PHP: Ternary operator (IF() statement) in MySQL and SQLite SQLite'i, MySQL PHP: Ternaarne operaator (IF () deklaratsioon) on MySQL ja SQLite

Posted on 03. Postitatud 03. Feb, 2010 by Dragos in Coding , MySQL , PHP , SQLite Veebruar, 2010 Dragos in kodeerimise, 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). Töötamise ajal proxy checker vahendiks üks mu projekte, olin hädas, kuidas täita tingimisi päringule minu SQLite'i andmebaas uuendatud veerus üksnes juhul, kui selle väärtus on suurem kui 0 (see oleks mõttetu minu puhul lasta skript uuendatud kolonni negatiivseid väärtusi).

In Mysql one would do like this (we use the ternary operator IF(to_check_expression>0,expression1,expression2) ): In Mysql keegi teha niimoodi (me kasutame kolmekomponentsete operaator 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. Kuid SQLite'i see meetod ei tööta. Instead I had to use the case when (condition) then expression1 else expression2 end method. Selle asemel oli mul kasutada juhul, kui (tingimus), siis expression1 muidu expression2 lõpuni viis.

The above query in MySQL can be rewritten like this in SQLite: Eespool päring MySQL saab ümber kirjutada, nagu seda SQLite:

update table_name set column_name=case when (column_name-1)>0 then (column_name-1) else 0 end

That's it. Ongi kõik. I hope it helps someone! Loodan, et see aitab keegi!

Translate this post Tõlgi see postitus


Related posts: Seonduvad postitused:

  1. Wordpress: Easily Reset Your Account Password Using MySQL and PHP Wordpress: hõlpsalt taastada oma konto parool kasutate MySQL ja PHP
  2. PHP Error: Call to a member function fetch_assoc() on a non-object in PHP error: Call to liikme funktsiooni fetch_assoc () kedagi objekti
  3. PHP: Script to extract one's contacts from email (Gmail, Yahoo,Hotmail,AOL…) and send invites – OpenInviter to go! PHP: Skript väljavõte oma kontaktid e-posti (Gmail, Yahoo, Hotmail, AOL ...) ja saata kutsub - OpenInviter minna!

    blog comments powered by Disqus blog comments powered by Disqus