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 Aggiornamenti via RSS | Email 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: Ternari ...

SQLite, MySQL, PHP: Ternary operator (IF() statement) in MySQL and SQLite SQLite, MySQL, PHP: operatore ternario (IF () dichiarazione) in MySQL e SQLite

Posted on 03. Posted on 03. Feb, 2010 by Dragos in Coding , MySQL , PHP , SQLite Feb, 2010 da 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). Mentre si lavora su uno strumento proxy checker per uno dei miei progetti, ho faticato su come eseguire una query condizionato il mio database SQLite per aggiornare una colonna solo se il suo valore è maggiore di 0 (sarebbe inutile nel mio caso, per consentire al script di aggiornamento la colonna con valori negativi).

In Mysql one would do like this (we use the ternary operator IF(to_check_expression>0,expression1,expression2) ): In Mysql si potrebbe fare come questo (che utilizza l'operatore ternario 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. Tuttavia, in SQLite questo metodo non funziona. Instead I had to use the case when (condition) then expression1 else expression2 end method. Invece ho dovuto usare il caso in cui (condizione) expression1 quindi expression2 altro fine metodo.

The above query in MySQL can be rewritten like this in SQLite: La query di cui sopra in MySQL può essere riscritto come questo 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! Spero che aiuta a qualcuno!

Translate this post Traduci questo post


Related posts: Related posts:

  1. Wordpress: Easily Reset Your Account Password Using MySQL and PHP Wordpress: Facilmente Reset Your Account password che utilizza MySQL e PHP
  2. PHP Error: Call to a member function fetch_assoc() on a non-object in PHP Errore: Chiamata a una funzione membro fetch_assoc () su un non-oggetto in

    blog comments powered by Disqus commenti del blog powered by Disqus