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! אני מקווה שזה עוזר למישהו!
Related posts: הודעות קשורות:












































