From the course: MySQL Essential Training

Unlock this course with a free trial

Join today to access over 24,500 courses taught by industry experts.

The IF function

The IF function

- [Bill] Hi, I'm Bill Weinman. MySQL supports a simple IF function. Here I've copied some SQL from the chapter three exercise file, and we'll be using the scratch database so I'll go ahead and I'll execute that, USE scratch. And we'll create a simple table here called booltest with two columns, both of them integers A and B, and we'll insert one row with the values one and zero. I'll go ahead and execute all of that, and you see that there's our result. We have column A and column B with the values one and zero respectively. The IF function is fairly simple, it takes three expressions. The first expression is a condition. If the condition is true, the second expression is returned, and if the condition is false, the third expression is returned. And so when I execute this first SELECT statement, you see we get TRUE because A is indeed less than five. And when I run the second SELECT statement, you see the result is FALSE because B is not greater than zero. As you can see, the IF…

Contents