使用LEFT JOIN更新MYSQL



问题;

在下面的MySQL查询中,生成ConnectionID列表的内部查询返回34个ConnectionID。当我将这两个查询作为更新一起运行时,查询结果表明只有14行受到影响。我对UPDATE函数的理解是否不正确?

备注;

我正在尝试更新我的数据库,以便对于connections.PartnerID等于Partners.PartnerID.的任何给定连接,合作伙伴表中的DefaultUserType等于连接表中的RoleID

查询;

    UPDATE Connections
    LEFT JOIN Partners
        ON Connections.PartnerID = Partners.PartnerID
SET Connections.RoleID = Partners.DefaultUserType
WHERE
    ConnectionID IN(
        SELECT
            sub_qu_1.ConnectionID
        FROM
            (
                SELECT DISTINCT
                    Connections.ConnectionID,
                    Connections.MemberID,
                    Connections.FriendID,
                    Connections.RoleID,
                    Partners.DefaultUserType,
                    IF(Connections.RoleID = Partners.DefaultUserType,0,1)AS not_equal
                FROM
                    Connections
                LEFT JOIN Contacts ON Connections.FriendID = Contacts.EEID
                LEFT JOIN Partners ON Contacts.PartnerID = Partners.PartnerID
                WHERE
                    Partners.DefaultUserType NOT IN (28,29,30)
            )AS sub_qu_1
        WHERE
            not_equal = 1
        AND DefaultUserType IS NOT NULL
    )

我希望你没有数百万条记录!使用Coalesce(val,0(,如果没有在合作伙伴中记录,则设置0

    UPDATE Connections
SET Connections.RoleID = coalesce((select Partners.DefaultUserType FROM Partners WHERE Connections.PartnerID = Partners.PartnerID LIMIT 1),0)
WHERE
ConnectionID IN(
    SELECT
        sub_qu_1.ConnectionID
    FROM
        (
            SELECT DISTINCT
                Connections.ConnectionID,
                Connections.MemberID,
                Connections.FriendID,
                Connections.RoleID,
                Partners.DefaultUserType,
                IF(Connections.RoleID = Partners.DefaultUserType,0,1)AS not_equal
            FROM
                Connections
            LEFT JOIN Contacts ON Connections.FriendID = Contacts.EEID
            LEFT JOIN Partners ON Contacts.PartnerID = Partners.PartnerID
            WHERE
                Partners.DefaultUserType NOT IN (28,29,30)
        )AS sub_qu_1
    WHERE
        not_equal = 1
    AND DefaultUserType IS NOT NULL
)

已更正!

相关内容

  • 没有找到相关文章

最新更新