jeudi 19 février 2015

MySQL IF deciding multiple fields?

Depending on a complicated condition, I need to insert one or the other set of fields.


What I would like to do is (pseudo code)



IF (condition) DO
INSERT INTO table set columnA= "foo", columnB = "bar"
ELSE
INSERT INTO table set columnA= "no foo", columnB = "no bar"
ENDIF


Unfortunately I can't use stored procedures and I have to fit this into a regular statement.


I could repeat the condition for every column



INSERT INTO table (columnA, columnB) SELECT IF(condition, "foo", "no foo"), IF(condition, "bar", "no bar")


But the conditiion itself is complex abd nested, so I'm trying not to repeat it.


Is there a syntax that lets a single IF or CASE decide multiple columns ?


Aucun commentaire:

Enregistrer un commentaire