情况如下:
id
,表person
为478
。partners
中处理合作伙伴。person_partners
。有时,有些人会添加新的合作伙伴,我想运行一个查询:
DELETE * FROM person_partners where id_person=478
INSERT INTO person_partners (id_person,id_partner) VALUES (478, SELECT id FROM partners)
(但此查询会给我这个错误:{{1 }})ERROR 1242 (21000): Subquery returns more than 1 row
person_partners
有什么想法吗?
答案 0 :(得分:1)
像BugFinder所说,“你似乎在问题中有自己的答案”
要修复ERROR 1242,你必须这样写:
INSERT INTO person_partners (id_person,id_partner)
SELECT '478', id FROM partners;
除了“只需插入尚未在person_person = 478的person_partners中的所有合作伙伴”,您就可以
INSERT IGNORE INTO person_partners (id_person,id_partner)
SELECT '478', id FROM partners;
详细了解here。
答案 1 :(得分:0)
INSERT INTO person_partners (id_person,id_partner) SELECT 478, ID FROM partners
ON DUPLICATE KEY UPDATE id_person=478;