1

我有以下查询,我希望它显示“尚未”,而不是显示数据库中 Status 列的 NULL 值。怎么做?

SELECT     dbo.SafetySuggestionsLog.Title, dbo.SafetySuggestionsLog.Description, dbo.SafetySuggestionsLog.Username, dbo.SafetySuggestionsStatus.Status,
                 CASE WHEN dbo.SafetySuggestionsStatus.Status  IS NULL THEN 'NOT YET'
FROM         dbo.SafetySuggestionsLog INNER JOIN
                      dbo.employee ON dbo.SafetySuggestionsLog.Username = dbo.employee.Username LEFT OUTER JOIN
                      dbo.SafetySuggestionsStatus ON dbo.SafetySuggestionsLog.StatusID = dbo.SafetySuggestionsStatus.ID
WHERE     (dbo.employee.Username = @Username)

供您参考,该数据库设计如下:

Employee Table: Username, Name
SafetySuggestionsLog Table: ID, Title, Description, Username, StatusID
SafetySuggestionsStatus: ID, Status
4

2 回答 2

2

使用COALESCEISNULL

SELECT     dbo.SafetySuggestionsLog.Title, dbo.SafetySuggestionsLog.Description, dbo.SafetySuggestionsLog.Username, COALESCE(dbo.SafetySuggestionsStatus.Status, "Not Yet") as 'Status'
FROM         dbo.SafetySuggestionsLog INNER JOIN
                      dbo.employee ON dbo.SafetySuggestionsLog.Username = dbo.employee.Username LEFT OUTER JOIN
                      dbo.SafetySuggestionsStatus ON dbo.SafetySuggestionsLog.StatusID = dbo.SafetySuggestionsStatus.ID
WHERE     (dbo.employee.Username = @Username)

或者

SELECT     dbo.SafetySuggestionsLog.Title, dbo.SafetySuggestionsLog.Description, dbo.SafetySuggestionsLog.Username, ISNULL(dbo.SafetySuggestionsStatus.Status, "Not Yet") as 'Status'
FROM         dbo.SafetySuggestionsLog INNER JOIN
                      dbo.employee ON dbo.SafetySuggestionsLog.Username = dbo.employee.Username LEFT OUTER JOIN
                      dbo.SafetySuggestionsStatus ON dbo.SafetySuggestionsLog.StatusID = dbo.SafetySuggestionsStatus.ID
WHERE     (dbo.employee.Username = @Username)
于 2012-06-25T09:26:56.993 回答
1

你几乎就在那里,你可以使用 ISNULL 或 CASE

CASE WHEN dbo.Blah.Status IS NULL THEN 'Not Yet' ELSE dbo.Blah.Status END

或者

isnull(dbo.Blah.Status, 'Not Yet')
于 2012-06-25T09:27:40.003 回答