带有postgres的不需要的sql返回空字符串

时间:2016-08-22 11:56:12

标签: postgresql

我在postgresql上有这个查询

SELECT 
    unnest(array[ sale_contact_name, purchase_contact_name, admin_contact_name]) AS name , 
    unnest(array[ sale_contact_mobile  , purchase_contact_mobile , admin_contact_mobile ]) AS mobile  ,
    unnest(array[ sale_contact_phone, purchase_contact_phone, admin_contact_phone]) AS phone
from company_t
where  companyCode = 'anything'   ;

如果没有数据,我会得到空字符串。 我不想得到这些空字符串,我怎么能这样做

1 个答案:

答案 0 :(得分:0)

您可以使用CASE语句来处理空字符串:

SELECT CASE WHEN t.name = ''   THEN 'Alt name'   ELSE t.name END,
       CASE WHEN t.mobile = '' THEN 'Alt mobile' ELSE t.mobile END,
       CASE WHEN t.phone = ''  THEN 'Alt phone'  ELSE t.phone END
FROM
(
    SELECT unnest(array[ sale_contact_name, purchase_contact_name, admin_contact_name]) AS name,
           unnest(array[ sale_contact_mobile, purchase_contact_mobile , admin_contact_mobile ]) AS mobile,
           unnest(array[ sale_contact_phone, purchase_contact_phone, admin_contact_phone]) AS phone
    FROM company_t
    WHERE companyCode = 'anything'
) t