Mysql:使用大小写仅列出非空值
我尝试使用查询仅列出NOT NULL值:Mysql:使用大小写仅列出非空值,mysql,case,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.E
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
的列,我确实会得到空值。我不确定我在案例中的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 |
+------------------+--------------+---------------------+----------------------------------+------------------+----------------+
获取空值的原因是您没有
ELSE
案例
因此,如果要在不满足任何条件时替换结果集中的NULL值
您只需添加和ELSE
case即可:
...
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)
您的CASE函数中没有编码ELSE。因此,如果有任何值使得条件为真时3个值都不存在,investorextrefid将设置为NULL。请尝试添加:
ELSE CONCAT(ent.EntityType',if(ISNULL(ind.ExtRefId),'NULL',ind.ExtRefId))
以查看ELSE情况。@Ronaldaronson感谢您的回复,我想的是,如果行本身为空,我怎么排除它呢?为什么我没有想到这一点?感谢您的回复,它成功了。:)
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)