使用 IN 和子查询进行 MYSQL 更新

发布于 2024-09-04 10:11:28 字数 777 浏览 10 评论 0原文

嗨,我有这样的表:

表条目:

id |总评论数
_____________________
1 | 0
2 | 0
3 | 0
4 | 0

表评论:

id |开斋节|评论
_____________________
1 | 1 |评论 sdfd
2 | 1 |测试测试
3 | 1 |评论文字
4 | 2 |虚拟评论
5 | 2 |示例评论
6 | 1 | fg fgh dfh

我写的查询:

UPDATE entry 
   SET total_comments = total_comments + 1 
 WHERE id IN ( SELECT eid 
                 FROM comments 
                WHERE id IN (1,2,3,4,5,6))

我得到的结果是:

表条目:

id |评论总数
_____________________
1 | 1
2 | 1
3 | 0
4 | 0

预期结果:

表条目:

id |评论总数
_____________________
1 | 4
2 | 2
3 | 0
4 | 0

任何帮助将不胜感激。

Hi i have tables like this :

table entry :

id | total_comments
_____________________
1 | 0
2 | 0
3 | 0
4 | 0

table comments :

id | eid | comment
_____________________
1 | 1 | comment sdfd
2 | 1 | testing testing
3 | 1 | comment text
4 | 2 | dummy comment
5 | 2 | sample comment
6 | 1 | fg fgh dfh

Query i write :

UPDATE entry 
   SET total_comments = total_comments + 1 
 WHERE id IN ( SELECT eid 
                 FROM comments 
                WHERE id IN (1,2,3,4,5,6))

Results i get is :

table entry :

id | total_comments
_____________________
1 | 1
2 | 1
3 | 0
4 | 0

Expected results :

table entry :

id | total_comments
_____________________
1 | 4
2 | 2
3 | 0
4 | 0

Any help will be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

悟红尘 2024-09-11 10:11:28

使用:

UPDATE entry 
   SET total_comments = (SELECT COUNT(*)
                           FROM COMMENTS c
                          WHERE c.eid = id
                       GROUP BY c.eid)
 WHERE id IN ( SELECT eid 
                 FROM comments 
                WHERE id IN (1,2,3,4,5,6))

Use:

UPDATE entry 
   SET total_comments = (SELECT COUNT(*)
                           FROM COMMENTS c
                          WHERE c.eid = id
                       GROUP BY c.eid)
 WHERE id IN ( SELECT eid 
                 FROM comments 
                WHERE id IN (1,2,3,4,5,6))
花开浅夏 2024-09-11 10:11:28

如果您确实需要在单独的表中添加total_comments,我会将其设为视图。

CREATE VIEW entry AS 
  SELECT id, COUNT(comments) AS total_comment 
  FROM comments 
  GROUP BY id

这样您就可以避免完全更新total_comments 表的维护任务。

If you really need total_comments in a separate table, I would make that a VIEW.

CREATE VIEW entry AS 
  SELECT id, COUNT(comments) AS total_comment 
  FROM comments 
  GROUP BY id

This way you avoid the maintenance task of updating the total_comments table altogether.

那伤。 2024-09-11 10:11:28

这正是我所期望的。 id 位于您给它的集合中,因此total_comments =total_comments + 1。

它不会为相同值的每个实例添加一个:这不是 IN 的工作方式。 IN 将返回一个简单的布尔值是/否。

That's exactly what I'd expect. The id is IN the set you give it, so total_comments = total_comments + 1.

It's not going to add one for each instance of the same value: that's not how IN works. IN will return a simple boolean yes/no.

小猫一只 2024-09-11 10:11:28

尝试:

UPDATE entry
  SET total_comments = (SELECT COUNT(*) 
                        FROM comments
                        WHERE entry.id = comments.eid
                        GROUP BY id)

Try:

UPDATE entry
  SET total_comments = (SELECT COUNT(*) 
                        FROM comments
                        WHERE entry.id = comments.eid
                        GROUP BY id)
梦断已成空 2024-09-11 10:11:28
UPDATE entry e 
    SET total_comments = ( SELECT COUNT(*) FROM comments WHERE eid = e.id)  
    WHERE 
    e.id in (SELECT eid FROM comments WHERE id IN (1,2,3,4,5,6))
UPDATE entry e 
    SET total_comments = ( SELECT COUNT(*) FROM comments WHERE eid = e.id)  
    WHERE 
    e.id in (SELECT eid FROM comments WHERE id IN (1,2,3,4,5,6))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文