MySQL if语句子查询值

MySQL if语句子查询值

问题描述:

我想做这样的事情:

SELECT IF((SELECT something FROM table) AS tmp) > 0, tmp, (SELECT bla FROM oter_table))

不幸的是'as tmp'和tmp的回归不管用。我怎样才能让它发挥作用?不重复查询:

Unfortunately the 'as tmp' and returing the tmp is not working. How can I get this to work? without doing repeating the query:

SELECT IF((SELECT something FROM table) > 0, (SELECT something FROM table), (SELECT bla FROM oter_table))


你可以用 用户定义的变量

SELECT IF(
          @val:=(SELECT something FROM table1) > 0, 
          @val, 
          (SELECT bla FROM table2)
       )

SQL小提琴