Mysql
 sql >> Datenbank >  >> RDS >> Mysql

Wie zeige ich Text in einem leeren Feld in einer SQL-Abfrage an?

Sie können einen CASE verwenden :

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);

Oder noch besser 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);

Wenn die Daten eine leere Zeichenfolge oder null enthalten, würde ich die Verwendung von:

in Betracht ziehen
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);