SQLite, MySQL, PHP: Ternary operator (IF() statement) in MySQL and SQLite SQLite, MySQL, PHP: operator de tripartită (IF () Declaraţie a) în MySQL şi SQLite
Posted on 03. Postat la data de 03. Feb, 2010 by Dragos in Coding , MySQL , PHP , SQLite Februarie, 2010 de Dragos în codificare a mărfurilor, 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). În timp ce lucra pe un instrument proxy pul pentru unul dintre proiectele mele, am fost lupta cu privire la modul de a executa o interogare condiţionată de bază de date SQLite meu pentru a actualiza o coloană numai în cazul în care valoarea sa este mai mare decât 0 (ar fi inutile în cazul meu pentru a lăsa script de actualizare coloana cu valori negative).
In Mysql one would do like this (we use the ternary operator IF(to_check_expression>0,expression1,expression2) ): Într-un Mysql ar face ca acesta (vom folosi operatorul ternare 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. Cu toate acestea, în SQLite această metodă nu va funcţiona. Instead I had to use the case when (condition) then expression1 else expression2 end method. În schimb am avut de a utiliza cazul in care (condiţie), apoi expression1 altceva expression2 sfârşitul metodă.
The above query in MySQL can be rewritten like this in SQLite: Interogare de mai sus, în MySQL poate fi rescrisă ca acesta, în SQLite:
update table_name set column_name=case when (column_name-1)>0 then (column_name-1) else 0 end
That's it. Asta e tot. I hope it helps someone! Sper că vă ajută pe cineva!
Related posts: Legate de posturi:
- Wordpress: Easily Reset Your Account Password Using MySQL and PHP Wordpress: resetată uşor Contul dvs. parolă, folosind PHP si MySQL
- PHP Error: Call to a member function fetch_assoc() on a non-object in PHP Error: Apel către un membru funcţie fetch_assoc () de pe un non-obiect în
- PHP: Script to extract one's contacts from email (Gmail, Yahoo,Hotmail,AOL…) and send invites – OpenInviter to go! PHP: Script pentru a extrage contacte unul de la e-mail (Gmail, Yahoo, Hotmail, AOL ...) şi a trimite a invita - OpenInviter sa plec!












































