Some of my SQL statement like this:
  IF(timediff(a.wo_finish_time,
               IF(a.wo_failure_time <> '0000-00-00 00:00:00',
                  a.wo_failure_time,
                  a.wo_creation_time)) < 0,
      0,
      timediff(a.wo_finish_time,
               IF(a.wo_failure_time <> '0000-00-00 00:00:00',
                  a.wo_failure_time,
                  a.wo_creation_time)))
As you can see, it is verbose because part of it appears twice. So I tried user defined variable like this:
 if(@down_time := timediff(a.wo_finish_time,
                             IF(a.wo_failure_time <> '0000-00-00 00:00:00',
                                a.wo_failure_time,
                                a.wo_creation_time)) < 0,
      0,
      @down_time)
Maybe due to the difference in variable scope, it doesn't work. I know I can put this @down_time variable declaration in SELECT list, but that would also add it to the output column. I guess there must be a better way, so I post this question, hoping to find a better approach.
