John
John

Reputation: 1015

mySQL Conditional Syntax Issue

Essentially I would like to determine a value based on a conditional and then use that value inside a different conditional all in the same select statement.

Example:

IF(IF (`date1` IS NOT NULL AND `date1 ` <> 0), 
`date1`, `date2` AS processdate) = curdate(), "ready", "not ready" AS action_status

To determine processdate I am saying check the field date1 has a date, if so use it, otherwise use date2 as processdate.

Once processdate is determined, compare it to todays date, if they are equal set action_status = "ready", otherwise "not ready".

I believe the syntax is incorrect, what is the correct format for something like this?

Upvotes: 1

Views: 20

Answers (1)

ysth
ysth

Reputation: 98398

AS can only apply to an entire expression for a return value of the select; you can't embed it in an expression. And each IF needs parentheses around its three arguments. So:

IF(
    curdate() =
        IF(
            `date1` IS NOT NULL AND `date1 ` <> 0, 
            `date1`,
            `date2`
        ),
    "ready",
    "not ready"
) AS action_status

(I changed ... = curdate() to be curdate() = ... just because I think it's more readable that way; either would work.)

Upvotes: 2

Related Questions