search

Home  >  Q&A  >  body text

mysql 的IFNULL失效

想要判断当查找的值找不到时给一个默认值,就试试IFNULL函数。
这样是可以查到的select IFNULL(NULL,'null') as name from game
输出结果:


但是当加了查询套件却出不来了:

select  IFNULL(NULL,'null') as name from game where id='sdfsf'

输出结果:

这里的id值是数据库中不存在的id,想要的结果是像第一个查询那样,显示为null

PHPzPHPz2874 days ago726

reply all(1)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-17 15:52:46

    The role of IFNULL() in MySQL:

    SELECT IFNULL(x, y)
    

    If x in the returned data recordis not NULL (does not contain 0), IFNULL() returns x, otherwise it returns y.

    Because the id value you query does not exist in the database, no record is returned, and IFNULL() will not work either. As for giving a default value when no records are returned, it is recommended to implement it programmatically.

    reply
    0
  • Cancelreply