-1

我想做这样的事情:

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

不幸的是,'as tmp' 和返回 tmp 不起作用。我怎样才能让它工作?无需重复查询:

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

2 回答 2

3

您可以使用User-Defined Variables

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

SQL小提琴

于 2013-07-18T12:59:27.907 回答
0
SELECT IF( (SELECT something AS tmp FROM first_table) > 0, tmp, (SELECT bla FROM other_table) )

编辑这是行不通的。您需要使用“WHERE”子句在某处量化选择,如下所示:

SELECT IF( (SELECT something FROM first_table) > 0, (SELECT something FROM first_table), (SELECT something_else FROM second_table) ) FROM first_table WHERE something = 45 
于 2013-07-18T12:54:26.280 回答