如何在我的sql表中获取数据时显示0而不是NULL

时间:2013-07-17 08:21:02

标签: mysql sql null

我从mysql表中获取数据它工作正常,但它在两个字段中显示NULL Status。我希望代替NULL显示0

这是我的问题:

SELECT ml.GroupID, ml.GroupTitle, ml.GroupDescription, ml.GroupCreatedDateTime, ml.GroupOwnerUserID, ml.ApprovalNeeded, ml.GroupStatus, ml.OrganizationCode, cat.Status
FROM GroupsMaster AS ml
LEFT JOIN UserGroupsMapping cat ON cat.GroupID = ml.GroupID
WHERE ml.OrganizationCode = ?

3 个答案:

答案 0 :(得分:8)

使用COALESCE运算符

   COALESCE(cat.Status, 0)


SELECT 
 ml.GroupID, 
 ml.GroupTitle, 
 ml.GroupDescription, 
 ml.GroupCreatedDateTime, 
 ml.GroupOwnerUserID, 
 ml.ApprovalNeeded, 
 ml.GroupStatus, 
 ml.OrganizationCode, 
 COALESCE(cat.Status, 0) as Status 
FROM GroupsMaster AS ml 
LEFT JOIN UserGroupsMapping cat ON cat.GroupID = ml.GroupID 
WHERE ml.OrganizationCode = 'ibm' 

答案 1 :(得分:0)

您可以尝试ifnull()功能。 如下:

选择 ifnull (cat.status,0)作为状态
FROM GroupsMaster AS ml LEFT JOIN UserGroupsMapping cat ON cat.GroupID = ml.GroupID WHERE ml.OrganizationCode ='$ organizationCode'“);

答案 2 :(得分:0)

使用此

SELECT 
 ml.GroupID, 
 ml.GroupTitle, 
 ml.GroupDescription, 
 ml.GroupCreatedDateTime, 
 ml.GroupOwnerUserID, 
 ml.ApprovalNeeded, 
 ml.GroupStatus, 
 ml.OrganizationCode, 
 COALESCE(cat.Status, 0) as Status 
FROM GroupsMaster AS ml 
LEFT JOIN UserGroupsMapping cat ON cat.GroupID = ml.GroupID 
and ml.OrganizationCode = 'ibm'