Home  >  Q&A  >  body text

mysql - 怎么让 SELECT 1+null 等于 1

怎么让

SELECT 1+null

返回的值等于 1

实际的sql可能是

-- 比如 a为1 b为null 
select `a` + `b` from `foo`;

我想的结果是最后select到的 a + b = 1

阿神阿神2742 days ago666

reply all(2)I'll reply

  • PHPz

    PHPz2017-04-17 15:00:58

    It works in mysql, and you can find the corresponding function in other DBs:
    select a + ifnull(b, 0) from foo;

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 15:00:58

    The correct answer upstairs, but I think from a database perspective, if an attribute needs to be able to be added and subtracted, then the default value should be set to 0 when designing the database, so that there is no need to use it. ifnul function

    reply
    0
  • Cancelreply