Jean Hoffmann
Jean Hoffmann

Reputation: 113

MYSQL - Is it possible to use a query's FIELD on an IF STATEMENT?

I would like to avoid tons of code just doing something like this:

SELECT IF (field1 = 1, field1, field2) AS field,
       IF (field = 1, true, false) AS field4
FROM table

I dont want to make IF inside IF to verify these things cos' there's a lot of cases like that.

Upvotes: 0

Views: 22

Answers (1)

JagaSrik
JagaSrik

Reputation: 718

I think, You can use an alternative 'case' statement like this

SELECT
case when  field1 = 1 then field1 
     when   field = 1 then field4
      ... more conditions...
      else Field2 end as Field
FROM table

more on click

Upvotes: 1

Related Questions