开发者

how to select an empty table, but get one row in result (NULL + number)?

For example, i have an empty table 'A' with columns: number(INT) and id.

I know that in MySQL null + number returns null. However there is workaround using IFNULL function:

SELECT IFNULL(NULL, 0) + @number

would return @number. But if I try to select from an existing table, results are not the same:

SELECT IFNULL(number, 0) + @number FROM A 

gives me a result with no rows selected.

How can i get MySQL to display one row with '开发者_如何学JAVAnumber' being a number, so I could insert minimal @number using REPLACE statement into table A?

Maybe there is another way..?


You can't - you're selecting from an empty table. The number of rows returned is directly related to the number of rows in your table selection.

If you expect just a single row in A at any time, you could do something like this:

select IFNULL((select number from A limit 1), 0) + @number

But, I'd have to ask the broader question of what exactly you're trying to achieve with this query, because it's likely that it can be done in a better way.


If this is indeed some aggregate function then something along these lines will always return 1 row

select MIN(IFNULL(tablesum.mysum, 0))
from 
  (select MIN(t.score) mysum from table t    
   union all 
   select @number as mysum
  ) tablesum

Reading your comment I think you want Select min of a table (0 or 1 row), combine that with a row with a fixed number then sum those 1 or 2 rows:

select SUM(IFNULL(d.number, 0))
from 
  (select MIN(t.number) number from table t    
   union all 
   select @number as number
  ) d
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜