繁体   English   中英

如果IFNULL不为NULL,则不会返回正确的字段值

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM