简体   繁体   中英

how to use mysql “IFNULL”

As far as I know IFNULL() function is

IFNULL(exp1,exp2),when exp1=null will output exp2

but I try a some condition like as this

select  IFNULL (ams2.deptno.`FName`,'empty')
  from ams2.deptno as dept

but result still null not empty

I don't know where have problem,

have anyone can teach me how to fix?

1.The query You've used is correct need to debug can you give some other info like your table's crate table statement,some sample data

mysql> select ifnull(FName,'empty') as FName from deptno;
+---------------------------------------------------------------------   ---------------------------+
| FName                                                                                              |
+---------------------------------------------------------------------  ---------------------------+
| myname                                                                                           |
| myname                                                                                         |
| myname                                                                                         |
| myname                                                                                         |
| myname                                                                                         |
| myname                                                                                         |
| myname                                                                                         |
| myname                                                                                         |
| myname                                                                                         |
| myname                                                                                         |
| empty                                                                                          |
| empty                                                                                          |
| empty                                                                                          |
| empty                                                                                          |
| empty                                                                                          |
| empty                                                                                            |
| iam                                                                                            |
| ifdsam                                                                                         |
| rwer                                                                                           

There are many other possible solutions which will fit your problem

1.Use the below Query

select COALESCE(ams2.deptno.`FName`,'empty') as dept 
from ams2.deptno

2.You can specify a default value for the column if the values are null

alter table ams2.deptno 
modify cloumn FName varchar(255) 
               not null default 'empty'
select  IFNULL(FName,'empty')
  from ams2.deptno;

Why dont you try this simple way without backticks, I tried its working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM