Mysql
 sql >> Base de données >  >> RDS >> Mysql

Comment afficher du texte dans un champ vide dans une requête sql ?

Vous pouvez utiliser un CASE :

select concat(first_name, " ", last_name) as CustomerName, 
   address, 
   case when address2 is null then 'None Provided' else address2 end as address2, 
   postal_code 
from customer
inner join address 
using (address_id);

Ou encore mieux COALESCE :

select concat(first_name, " ", last_name) as CustomerName, 
   address, 
   COALESCE(address2,'None Provided') as address2, 
   postal_code 
from customer
inner join address 
using (address_id);

Si les données contiennent une chaîne vide ou nulle, alors j'envisagerais d'utiliser :

select concat(first_name, " ", last_name) as CustomerName, 
   address, 
   case 
     when address2 is null or address2 = '' 
     then 'None Provided' 
     else address2 end as address2, 
   postal_code 
from customer
inner join address 
using (address_id);