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: tatlong bagay ...

SQLite, MySQL, PHP: Ternary operator (IF() statement) in MySQL and SQLite SQLite, MySQL, PHP: tatluhan operator (KUNG () pahayag) sa MySQL at SQLite

Posted on 03. Posted on 03. Feb, 2010 by Dragos in Coding , MySQL , PHP , SQLite Pebrero, 2010 sa pamamagitan ng Dragos sa 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). Habang nagtatrabaho sa isang proxy checker tool para sa isa sa aking mga proyekto, ako ay struggling sa kung paano magpatakbo ng isang kondisyon na query sa aking SQLite database upang i-update ang isang haligi na lamang kung ang halaga nito ay mas malaki kaysa sa 0 (Gusto na ito ay walang kahulugan sa aking mga kaso na pabayaan ang mga script na i-update ang haligi na may mga negatibong mga halaga).

In Mysql one would do like this (we use the ternary operator IF(to_check_expression>0,expression1,expression2) ): Sa MySQL isa Gusto ko ganito (gamitin natin ang tatlong bagay operator KUNG (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. Gayunman, sa SQLite ang pamamaraan na ito ay hindi gagana. Instead I had to use the case when (condition) then expression1 else expression2 end method. Sa halip na ako ay nagkaroon na gamitin ang kaso kapag ang (kalagayan) pagkatapos expression1 ibang expression2 dulo paraan.

The above query in MySQL can be rewritten like this in SQLite: Ang mga tanong sa itaas sa MySQL ay maaaring muling pagsusulat ng katulad nito sa 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! Umaasa ako na ito ay tumutulong sa isang tao!

Translate this post Isalin ang post na ito


Related posts: Related posts:

  1. Wordpress: Easily Reset Your Account Password Using MySQL and PHP Wordpress: Madaling I-reset ang iyong Account Password Paggamit ng MySQL at PHP
  2. PHP Error: Call to a member function fetch_assoc() on a non-object in PHP Error: Tawag sa isang miyembro fetch_assoc function () sa isang hindi-bagay sa

    blog comments powered by Disqus blog comments powered by Disqus