问题;
在下面的MySQL查询中,生成ConnectionID列表的内部查询返回34个ConnectionID。然后,当我将两个查询作为更新一起运行时,查询结果表明只有14行受到影响。我对UPDATE函数如何不正确的理解是什么?
注释;
我正在尝试更新我的数据库,以便Partners表中的DefaultUserType等于Connections表中的RoleID,用于Connections.PartnerID等于Partners.PartnerID的任何给定连接。
查询;
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
)
答案 0 :(得分:0)
我希望你没有数百万条记录! 使用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
)
更正!