通过检查null是否显示不同的文本

时间:2013-12-26 06:57:24

标签: mysql sql

我正在使用mysql数据库。我有一个数据库lms_system与表supplier_details。如何查询以显示供应商ID,供应商名称和联系人,如果电话号码为空,则显示“否”,否则显示“是”,别名为“PHONENUMAVAILABLE”。

4 个答案:

答案 0 :(得分:2)

有几种方法可以做到这一点

SELECT id, name, contact, IF(phone IS NULL, 'No', 'Yes') phonenumavailable
  FROM supplier_details

SELECT id, name, contact, CASE WHEN phone IS NULL THEN 'No' ELSE 'Yes' END phonenumavailable
  FROM supplier_details

SELECT id, name, contact, ELT((phone IS NULL) + 1, 'Yes', 'No') phonenumavailable
  FROM supplier_details

SELECT id, name, contact, COALESCE(REPLACE(phone, phone, 'Yes'), 'No') phonenumavailable
  FROM supplier_details

这是 SQLFiddle 演示

答案 1 :(得分:0)

SELECT 
       ......
        , IF(phone_number is null, 'NO', 'YES') 
FROM  ....

答案 2 :(得分:0)

SELECT  [Your Others Columns],
IF(PHONENUMAVAILABLE IS NULL,'NO','YES') 
[Your Table Name]

答案 3 :(得分:0)

使用IFdocumentation

SELECT ... IF(`your_field` IS NULL,'NO','YES) as PHONENUMAVAILABLE ... FROM...

或使用CASEdocumentation

SELECT ...
    CASE WHEN `your_field` IS NOT NULL 
           THEN 'YES'
           ELSE 'NO'
    END AS PHONENUMAVAILABLE
FROM ...

还有IFNULL()documentation

SELECT ... IFNULL(`your_field`, 'NO') as PHONENUMAVAILABLE ... FROM ...

但如果your_field不为空

,则不会返回YES