从另一个 table SQL 的值更新列值
Updating a column value from the value of the other table SQL
我有一个 table chatmessages
,其中包含 chatId、incomingId、outgoingId 列,我还有另一个 table chatmembers
,其中包含 chatId、usersId 列。还有另一个 table chat
,它只包含 chatId,以及聊天的 usersId 初始值设定项,即 user1
现在,我想将 chatId 从 chatmembers
复制到 chatmessages
其中有相同的用户,例如, incomingId 83 和 outgoingId 81 在 [=14 中具有相同的 chatId =]
我有这个查询,但它不起作用
UPDATE chatmessages SET chatId =
(SELECT c.chatId FROM chat AS c INNER JOIN (SELECT chatId FROM chatmembers WHERE usersId = incomingId) AS uc ON c.chatId = uc.chatId
INNER JOIN (SELECT chatId FROM chatmembers WHERE companyId = outgoingId) AS cc ON uc.chatId = cc.chatId)
如果我没理解错的话,你可以使用join
s。以下使用 MySQL 语法:
update chatmessages cm join
chatusers cuo
on cuo.userid = cm.outgoingid join
chatusers cui
on cui.userid = cm.incomingid and
cui.chatid = cuo.chatid
set cm.chatid = cuo.chatid;
您必须将此查询写入数组,然后执行,
在
中也使用双引号
(SELECT c.chatId FROM chat AS c
INNER JOIN (SELECT chatId FROM chatmembers WHERE usersId = incomingId) AS uc ON c.chatId = uc.chatId
INNER JOIN (SELECT chatId FROM chatmembers WHERE companyId = outgoingId)
像这样
("SELECT c.chatId FROM chat AS c
INNER JOIN (SELECT chatId FROM chatmembers WHERE usersId = incomingId) AS uc ON c.chatId = uc.chatId
INNER JOIN (SELECT chatId FROM chatmembers WHERE companyId = outgoingId")
我有一个 table chatmessages
,其中包含 chatId、incomingId、outgoingId 列,我还有另一个 table chatmembers
,其中包含 chatId、usersId 列。还有另一个 table chat
,它只包含 chatId,以及聊天的 usersId 初始值设定项,即 user1
现在,我想将 chatId 从 chatmembers
复制到 chatmessages
其中有相同的用户,例如, incomingId 83 和 outgoingId 81 在 [=14 中具有相同的 chatId =]
我有这个查询,但它不起作用
UPDATE chatmessages SET chatId =
(SELECT c.chatId FROM chat AS c INNER JOIN (SELECT chatId FROM chatmembers WHERE usersId = incomingId) AS uc ON c.chatId = uc.chatId
INNER JOIN (SELECT chatId FROM chatmembers WHERE companyId = outgoingId) AS cc ON uc.chatId = cc.chatId)
如果我没理解错的话,你可以使用join
s。以下使用 MySQL 语法:
update chatmessages cm join
chatusers cuo
on cuo.userid = cm.outgoingid join
chatusers cui
on cui.userid = cm.incomingid and
cui.chatid = cuo.chatid
set cm.chatid = cuo.chatid;
您必须将此查询写入数组,然后执行, 在
中也使用双引号(SELECT c.chatId FROM chat AS c
INNER JOIN (SELECT chatId FROM chatmembers WHERE usersId = incomingId) AS uc ON c.chatId = uc.chatId
INNER JOIN (SELECT chatId FROM chatmembers WHERE companyId = outgoingId)
像这样
("SELECT c.chatId FROM chat AS c
INNER JOIN (SELECT chatId FROM chatmembers WHERE usersId = incomingId) AS uc ON c.chatId = uc.chatId
INNER JOIN (SELECT chatId FROM chatmembers WHERE companyId = outgoingId")