本文介绍了MySQL更新子集具有的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有三个表:contacts,domains和contacts_domains,它们形成了多对多关系.
I have three tables: contacts, domains, and contacts_domains, which form a many-to-many relationship.
我想运行一个查询,以更新contacts_domains表,但仅适用于仅具有一个联系人的域.
I would like to run a query that updates the contacts_domains table, but only for domains that have exactly one contact.
我知道如何选择我感兴趣的行,但不知道如何更新它们.
I know how to SELECT the rows I'm interested in, but not how to UPDATE them.
SELECT domain_id, contact_id, dominant
FROM contacts_domains
GROUP BY domain_id
HAVING COUNT(contact_id) = 1
我想为所有这些结果设置contacts_domains.dominant = 1.
I want to set contacts_domains.dominant = 1 for all these results.
谢谢!
推荐答案
最简单的解决方案:
UPDATE contacts_domains cd SET cd.dominant = 1
WHERE cd.id IN (
SELECT dominant_id
FROM contacts_domains
GROUP BY domain_id
HAVING COUNT(contact_id) = 1
)
已编辑.
这篇关于MySQL更新子集具有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!