好的,我将尝试再次提出这个问题。我的上一篇文章已关闭,因为它不是一个更详细的问题,所以我会尝试更具体。
用户收到消息并标记状态unread
或read
基于他们是否点击了他们已阅读消息的帽子。
好吧,有些用户说他们收到了重复的消息。问题是跟踪消息的记录在第二个表中。所以两张桌子看起来像这样
主题表
id user_id message_id created status
1 34 81 03/11/12 read
2 34 82 06/15/12 unread
3 34 83 06/16/12 unread
4 35 84 07/10/12 read
5 35 85 07/12/12 unread
6 35 86 07/14/12 unread
7 35 87 07/14/12 unread
8 24 88 08/09/12 read
消息表
id message
81 'welcome'
82 'welcome' //duplicate for user 34, associated table needs to be set to read
83 'welcome' //duplicate for user 34, this record needs to be set to read
84 'welcome'
85 'welcome'
86 'welcome'
87 'pretty cool' //not a duplicate for user 35
88 'welcome' // not a duplicate for user 24
这个问题与第一个问题的不同之处在于这里有一个关联表。我需要遍历每个表,并且只更新具有重复消息的表。看到 message_table 中的记录 87?好吧,没有重复,所以被忽略了。
所以我的问题是如何根据与关联表的重复来更新表 1。
[编辑]
当我的意思是重复时,我的意思是所有具有相同消息值的关联消息的用户。所以看这个例子。user_id
34 有 3 条记录。如果我们查找message_id
每条记录,我们会发现相同的消息“欢迎”重复了 3 次。这是user_id
34的重复记录
以下为失败
UPDATE subject_table
SET `status` = 'read'
WHERE
user_id IN (
SELECT
s.user_id
FROM
subject_table s
INNER JOIN messages m ON m.id = s.message_id
WHERE
m.message LIKE '%welcome%'
GROUP BY
s.user_id
HAVING
count(s.user_id) > 1);
我在这里尝试做的是在我的子查询中,获取具有多个重复记录的 id 列表,然后更新关联的记录状态以读取。但是我遇到的问题是以下错误
Error : You can't specify target table 'subject_table' for update in FROM clause