SQLite, MySQL, PHP: Ternary operator (IF() statement) in MySQL and SQLite SQLite, MySQL, PHP: ternaire operator (ALS () statement) in MySQL en SQLite
Posted on 03. Geplaatst op 03. Feb, 2010 by Dragos in Coding , MySQL , PHP , SQLite Februari, 2010 door 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). Tijdens het werken aan een proxy checker tool voor een van mijn projecten was ik worstelen over hoe u een voorwaardelijke query uit te voeren op mijn SQLite database een kolom alleen als de waarde groter is dan 0 (het zou in mijn geval geen zin om de update laten script update van de kolom met negatieve waarden).
In Mysql one would do like this (we use the ternary operator IF(to_check_expression>0,expression1,expression2) ): In een Mysql zou doen als dit (we gebruiken de ternaire operator ALS (to_check_expression> 0, expressie1, expressie2)):
update table_name set column_name=IF((column_name-1)>0,(column_name-1),0)
However, in SQLite this method will not work. Echter, in SQLite deze methode zal niet werken. Instead I had to use the case when (condition) then expression1 else expression2 end method. In plaats daarvan moest ik gebruik het geval wanneer (voorwaarde) dan expressie1 anders expressie2 einde methode.
The above query in MySQL can be rewritten like this in SQLite: De bovenstaande query in MySQL herschreven kan worden als dit in 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! Ik hoop dat het helpt iemand!
Related posts: Gerelateerde berichten:
- Wordpress: Easily Reset Your Account Password Using MySQL and PHP Wordpress: Gemakkelijk Reset Uw account wachtwoord in met MySQL en PHP
- PHP Error: Call to a member function fetch_assoc() on a non-object in PHP error: Call to a member function fetch_assoc () op een niet-object in
- PHP: Script to extract one's contacts from email (Gmail, Yahoo,Hotmail,AOL…) and send invites – OpenInviter to go! PHP: Script om je contacten van e-mail (Gmail, Yahoo, Hotmail, AOL ...) extract en stuur nodigt - OpenInviter te gaan!












































