[英]IFNULL doesn't return the correct field value if its not NULL
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
即使该字段具有值且不为NULL,此查询仍会向ALIAS返回“ -1”,
从图像中可以看到,如果该字段不为NULL,它将返回(1)。 或我只是误解了IFNULL函数。
IFNULL()逐行工作,在home_ownerID下看到NULL的每一行上,因此IFNULL()在ownerid列中正确返回负一。 只有一行,其中home_ownerID有一个值,并且正确地IFNULL() 仅为该行返回该值
您可能在想像,IFNULL()可以以某种方式神奇地用该单个值填充每一行,但它无法做到这一点。
如果您还希望在该列中使用非NULL值,则应如下所示:
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.