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 עדכונים דרך RSS | אימייל Get updates via feedburner Get updates via twitter
Home / Coding / MySQL / PHP / SQLite / SQLite, MySQL, PHP: Ternary … דף הבית / Coding / MySQL / PHP / SQLite / SQLite, MySQL, PHP: טרינארית ...

SQLite, MySQL, PHP: Ternary operator (IF() statement) in MySQL and SQLite SQLite, MySQL, PHP: מפעיל טרינארית (IF () הצהרה) ב MySQL ו-SQLite

Posted on 03. פורסם ב 03. Feb, 2010 by Dragos in Coding , MySQL , PHP , SQLite פבואר, 2010 על ידי Dragos ב וקידוד, 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). תוך כדי עבודה על הכלי בודק Proxy עבור אחד הפרויקטים שלי, אני נאבק על איך לבצע שאילתה על מסד נתונים SQLite מותנה שלי לעדכן את עמודת הערך שלו רק אם הוא גדול מ -0 (זה יהיה חסר טעם במקרה שלי לתת סקריפט לעדכן את עמודה עם ערכים שליליים).

In Mysql one would do like this (we use the ternary operator IF(to_check_expression>0,expression1,expression2) ): ב Mysql אחד לא היה עושה ככה (אנו משתמשים מפעיל של שלושה 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. עם זאת, ב SQLite שיטה זו לא תעבוד. Instead I had to use the case when (condition) then expression1 else expression2 end method. במקום הייתי צריך להשתמש כאשר המקרה (מצב) ואז expression1 אחר expression2 סוף השיטה.

The above query in MySQL can be rewritten like this in SQLite: את השאילתה ב-MySQL לעיל ניתן לכתוב מחדש כזה SQLite:

update table_name set column_name=case when (column_name-1)>0 then (column_name-1) else 0 end

That's it. זהו זה. I hope it helps someone! אני מקווה שזה עוזר למישהו!

Translate this post תרגם פירסום


Related posts: הודעות קשורות:

  1. Wordpress: Easily Reset Your Account Password Using MySQL and PHP וורדפרס: בקלות איפוס סיסמת חשבון שלך באמצעות MySQL ו-PHP
  2. PHP Error: Call to a member function fetch_assoc() on a non-object in PHP שגיאה: הסב חבר פונקציה fetch_assoc () on a non-האובייקט

    blog comments powered by Disqus תגובות לבלוג powered by Disqus