SELECT *, IFNULL(h.house_ownerID, -1) AS ownerid
FROM house_players AS h
INNER JOIN house_interiors AS hi
ON h.house_interiorID = hi.house_intLevel即使该字段具有值并且不为空,该查询仍将'-1‘返回给别名,

正如您从图像中看到的,如果字段不为空,则应返回(1)。或者我只是误解了IFNULL函数。
发布于 2017-11-20 10:35:35
IFNULL()逐行工作,在home_ownerID下看到NULL的每一行上,所以IFNULL()在ownerid列中正确地返回负1。只有一行home_ownerID有一个值,IFNULL()正确地返回该行的值,只返回

您可能会想象,不知何故,IFNULL()可以用单个值填充每一行,但它做不到这一点。
发布于 2017-11-20 10:41:37
如果您希望列中也有一个非空值,则如下所示:
SELECT h.*, hi.*,
COALESCE(h.house_ownerID,
(SELECT h2.house_ownerId FROM house_players h2 WHERE h2.house_interiorID = h.house_interiorID)
) AS ownerid
FROM house_players h INNER JOIN
house_interiors hi
ON h.house_interiorID = hi.house_intLevel;https://stackoverflow.com/questions/47383924
复制相似问题