2013-07-17 59 views
1

我正在從mysql表中取數據,但它在兩個字段中顯示NULLStatus。我想代替NULL顯示0如何在讀取數據時顯示0而不是NULL我的sql表

這裏是我的查詢:

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 = ? 

回答

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' 
+0

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

+0

請參閱我的查詢,正如你所說的使用它不起作用 – user2240329

+0

@ user2240329你仍然有一個cat.Status丟失在你的代碼中(和/或缺少逗號)。看到我編輯的答案 –

0

你可以試試ifnull()功能。 像belowing:

選擇IFNULL(cat.status,0)作爲狀態
FROM GroupsMaster AS毫升 左連接UserGroupsMapping貓ON cat.GroupID = ml.GroupID WHERE ml.OrganizationCode = '$ organizationCode' 「);

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' 
相關問題