简体   繁体   中英

How can I replace value with a different value in MySQL?

I already change the NULL to a different value. My problem now is how can I change if there is value? Can somebody help me with my problem?

ID  Name
1   John
2   
3   Doe

I want this to happen when I display it to my page...

ID  Name
1   Secret
2   NULL
3   Secret

This is my query

SELECT id AS ID, COALESCE(first_name, 'NULL') AS Name FROM tbl_user

This should do the trick:

SELECT
    id,
    CASE WHEN name IS NULL THEN 'NULL' ELSE 'Secret' END Name
FROM mytable

This demo on DB Fiddle with your sample data returns:

id | Name  
-: | :-----
 1 | Secret
 2 | NULL  
 3 | Secret

你在找吗

select id, case when first_name is null then 'NULL' else 'Secret' end as Name

Here's another answer. using CASE and IFNULL

SELECT id AS ID
   , CASE WHEN IFNULL(first_name, '') = '' THEN 'NULL' ELSE first_name END AS Name 
FROM tbl_user

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