MySQL:如果字段为空,则不显示特定数据

时间:2019-02-18 12:40:29

标签: mysql

我有以下简单查询:

SELECT Customer_FirstName, CONCAT('*', Customer_Email, '*') 
from Customers;

它将显示如下内容:

John *jon@gmail.com*
Tash **
Luis **

如您所见,当表中没有电子邮件地址时,它仅显示“ **”,看起来很奇怪。我想显示以下内容:

John *jon@gmail.com*
Tash
Luis

如何省略整个“ *电子邮件*”?如果我不打印星号,那么我可以简单地使用IFNULL(Customers_Email,'')

问题是我需要显示星号。

2 个答案:

答案 0 :(得分:2)

您可以使用CASE WHEN表达式

SELECT Customer_FirstName, case when Customer_Email is not null then CONCAT('*', Customer_Email, '*') end
from Customers;

答案 1 :(得分:2)

使用此:

   SELECT Customer_FirstName, IF(Customer_Email IS NULL OR Customer_Email = '','',CONCAT('*',Customer_Email, '*')) as Customer_Email 
   from Customers;