MySQL的:只列出不使用情况的空值

时间:2019-06-19 13:08:11

标签: mysql case

我正在尝试使用查询仅列出NOT NULL值:

SELECT 
        firm.ShortName AS companyshortname,
        firm.ExtRefId AS compextrefid,
        finrnd.dealDate AS dealdate,
        (CASE
            WHEN (ent.EntityType = 'Firm') THEN afirm.ShortName
            WHEN (ent.EntityType = 'Partnership') THEN pship.ShortName
            WHEN
                (ent.EntityType = 'Individual')
            THEN
                CONCAT(ind.LastName,
                        ', ',
                        ind.FirstName)
        END) AS investor,
        (CASE
            WHEN (ent.EntityType = 'Firm' AND afirm.ExtRefId IS NOT NULL)  THEN afirm.ExtRefId
            WHEN (ent.EntityType = 'Partnership' AND pship.ExtRefId IS NOT NULL) THEN pship.ExtRefId
            WHEN (ent.EntityType = 'Individual' AND ind.ExtRefId IS NOT NULL) THEN ind.ExtRefId
        END) AS investorextrefid,
         finrndinv.isLeadInvestor AS isleadinvestor
    FROM
        ((((((financinginvestors finrndinv
        JOIN financingrounds finrnd ON ((finrnd.financingid = finrndinv.financingid)))
        JOIN firms firm ON ((firm.FirmEntID = finrnd.compentid)))
        LEFT JOIN entities ent ON ((ent.InvEntID = finrndinv.investorid)))
        LEFT JOIN firms afirm ON ((afirm.FirmEntID = ent.InvEntID)))
        LEFT JOIN individuals ind ON ((ind.IndEntID = ent.InvEntID)))
        LEFT JOIN partnerships pship ON ((pship.PShipEntID = ent.InvEntID)));

但是在运行代码时,我确实获得了名为investorextrefid的列的NULL值。我不确定在CASE的WHEN子句中添加的条件是否正确。该查询给出的结果与案例中WHEN子句中没有AND部分的结果相同。

+------------------+--------------+---------------------+----------------------------------+------------------+----------------+
| companyshortname | compextrefid | dealdate            | investor                         | investorextrefid | isleadinvestor |
+------------------+--------------+---------------------+----------------------------------+------------------+----------------+
| Access Insurance | ASP:143      | 2019-06-06 00:00:00 | ASP                              | NULL             |              0 |
| Access Insurance | ASP:143      | 2019-06-06 00:00:00 | Russell 2000                     | NULL             |              0 |
| Access Insurance | ASP:143      | 2019-06-06 00:00:00 | Addiko Bank                      | ASP:62           |              0 |
| Access Insurance | ASP:143      | 2019-06-06 00:00:00 | Universal American Financial Cor | ASP:119          |              0 |
| Access Insurance | ASP:143      | 2019-06-06 00:00:00 | Lever, Inc                       | ASP:131          |              1 |
+------------------+--------------+---------------------+----------------------------------+------------------+----------------+

1 个答案:

答案 0 :(得分:2)

获得NULL值的原因是因为您没有ELSE大小写

https://dev.mysql.com/doc/refman/5.7/en/case.html

CASE
    WHEN search_condition THEN statement_list
    [WHEN search_condition THEN statement_list] ...
    [ELSE statement_list] END CASE

因此,如果您不满足任何条件时要替换结果集中的NULL值 您应该添加ELSE大小写:

...
WHEN (ent.EntityType = 'Individual' AND ind.ExtRefId IS NOT NULL) THEN ind.ExtRefId 
ELSE 'Some other value' 
END) AS investorextrefid,
...

另一方面,如果您想完全省略这些行,则应在查询末尾放置WHERE条件,如下所示:

WHERE 
(ent.EntityType = 'Firm' AND afirm.ExtRefId IS NOT NULL) 
OR (ent.EntityType = 'Partnership' AND pship.ExtRefId IS NOT NULL) 
OR (ent.EntityType = 'Individual' AND ind.ExtRefId IS NOT NULL)