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 | 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: tresh ...

SQLite, MySQL, PHP: Ternary operator (IF() statement) in MySQL and SQLite SQLite, MySQL, PHP: operator tresh (NESE () deklaratë) në MySQL dhe SQLite

Posted on 03. Dërguar të 03. Feb, 2010 by Dragos in Coding , MySQL , PHP , SQLite Shkurt, 2010 nga Dragoskodim, 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). Duke punuar në një mjet proxy checker për një nga projektet e mia, unë kam qenë duke luftuar se si të ekzekutojë një pyetje të kushtëzuar për SQLite database e mia për të rinovuar një kolonë vetëm në qoftë se vlera e saj është më i madh se sa 0 (kjo do të ishte e pakuptimtë në rastin tim për të lejuar script update kolonë me vlera negative).

In Mysql one would do like this (we use the ternary operator IF(to_check_expression>0,expression1,expression2) ): Në MySQL një do të bënin si ky (ne përdorim operatorin tresh NESE (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. Megjithatë, në SQLite metodë kjo nuk do të funksionojë. Instead I had to use the case when (condition) then expression1 else expression2 end method. Në vend të kësaj më duhej të përdorni rastin kur (kusht) pastaj expression1 tjetër expression2 fund metodë.

The above query in MySQL can be rewritten like this in SQLite: Pyetjen e mësipërme në MySQL mund të rishkruhen si kjo në 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! Unë shpresoj se ndihmon dikush!

Translate this post Translate this post


Related posts: Related posts:

  1. Wordpress: Easily Reset Your Account Password Using MySQL and PHP Wordpress: Easily Reset Your Account Fjalëkalimi Përdorimi i MySQL dhe PHP
  2. PHP Error: Call to a member function fetch_assoc() on a non-object in PHP Error: Call to a member function fetch_assoc () në një vend jo-objekt në

    blog comments powered by Disqus Komentet blog powered by Disqus