Replace returned null values in LEFT OUTER JOIN

淺唱寂寞╮ 提交于 2020-01-14 08:50:11

问题


SELECT        WO_BreakerRail.ID, indRailType.RailType, indRailType.RailCode, WO_BreakerRail.CreatedPieces, WO_BreakerRail.OutsideSource, WO_BreakerRail.Charged, WO_BreakerRail.Rejected, WO_BreakerRail.RejectedToCrop, WO_BreakerRail.Date
FROM            indRailType LEFT OUTER JOIN
                         WO_BreakerRail ON indRailType.RailCode = WO_BreakerRail.RailCode AND WO_BreakerRail.Date = @date

When this returns, there are NULL values in the Date column where there are no matching rows from WO_BreakerRail. Is there a way to replace just those NULL values with @date?


回答1:


In oracle and sql server, you can use the COALESCE (oracle version) function

SELECT ...., COALESCE(WO_BreakerRail.Date, @date)



回答2:


You can use COALESCE function (go here for mysql documentation)

COALESCE(WO_BreakerRail.Date, @date)

or you can use a simple IF:

IF(WO_BreakerRail.Date IS NULL, @date, WO_BreakerRail.Date)



回答3:


Yes, just use COALESCE, eg. COALESCE(WO_BreakerRail.Date, @date)




回答4:


Actually, in MySQL there is a IFNULL() function.

select
  ifnull(wo_breakerrail.date, @date) as `date`,


来源:https://stackoverflow.com/questions/2507300/replace-returned-null-values-in-left-outer-join

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!