SQLite, MySQL, PHP: Ternary operator (IF() statement) in MySQL and SQLite SQLite, MySQL, PHP: การ Ternary (IF () แถลง) ใน MySQL และ SQLite
Posted on 03. โพสต์ใน 03. Feb, 2010 by Dragos in Coding , MySQL , PHP , SQLite Feb, 2,010 โดย 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). ขณะที่ทำงานกับเครื่องมือตรวจสอบการมอบฉันทะหนึ่งในโครงการของฉันฉันถูกดิ้นรนในการรันการสืบค้นเงื่อนไขในฐานข้อมูลของ SQLite เพื่อปรับปรุงคอลัมน์เฉพาะในกรณีที่ค่าของมันมากกว่า 0 (มันจะไม่มีจุดหมายในกรณีของฉันเพื่อให้ script update คอลัมน์ที่มีค่านิยมเชิงลบ).
In Mysql one would do like this (we use the ternary operator IF(to_check_expression>0,expression1,expression2) ): ใน MySQL หนึ่งจะทำเช่นนี้ (เราจะใช้ประกอบ ternary 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 end.
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: บทความที่เกี่ยวข้อง:












































