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 Päivitykset RSS | Sähköposti Get updates via feedburner Get updates via twitter
Home / Coding / MySQL / PHP / SQLite / SQLite, MySQL, PHP: Ternary … Etusivu / koodaus / MySQL / PHP / SQLite / SQLite, MySQL, PHP: kolmiravinteiset ...

SQLite, MySQL, PHP: Ternary operator (IF() statement) in MySQL and SQLite SQLite, MySQL, PHP: ternary operaattori (JOS () lausuma) MySQL-ja SQLite

Posted on 03. Postitettu 03. Feb, 2010 by Dragos in Coding , MySQL , PHP , SQLite Helmikuu, 2010 Dragos vuonna 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). Työskennellessään valtakirja tarkistus-työkalun yksi projektini, olin kamppailevat siitä, miten toteuttaa ehdollisen kyselyn minun SQLite tietokanta päivittää sarakkeeseen vain, jos sen arvo on suurempi kuin 0 (se olisi turhaa minun tapauksessani antaa script päivittää sarakkeen negatiiviset arvot).

In Mysql one would do like this (we use the ternary operator IF(to_check_expression>0,expression1,expression2) ): MySQL-voisi tehdä tällaista (käytämme ternary toimija IF (to_check_expression> 0, lauseke1, lauseke2)):

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

However, in SQLite this method will not work. Kuitenkin SQLite tämä menetelmä ei toimi. Instead I had to use the case when (condition) then expression1 else expression2 end method. Sen sijaan minun täytyi käyttää silloin, kun (ehto) niin lauseke1 muu lauseke2 loppuun menetelmällä.

The above query in MySQL can be rewritten like this in SQLite: Edellä kysely MySQL voidaan kirjoittaa tällaisena 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! Toivottavasti se auttaa joku!

Translate this post Käännä tämä viesti


Related posts: Liittyvien virkojen:

  1. Wordpress: Easily Reset Your Account Password Using MySQL and PHP WordPress: helposti palauttaa tilisi salasanan MySQL ja PHP
  2. PHP Error: Call to a member function fetch_assoc() on a non-object in PHP error: Call to jäsenen toiminnon fetch_assoc () ei-objektin
  3. PHP: Script to extract one's contacts from email (Gmail, Yahoo,Hotmail,AOL…) and send invites – OpenInviter to go! PHP: Script purkaa oman yhteystiedon sähköposti (Gmail, Yahoo, Hotmail, AOL ...) ja lähettää kutsuja - OpenInviter mennä!

    blog comments powered by Disqus blogin kommentit powered by Disqus