开发者

MySQL update many-to-many tabel relation

开发者 https://www.devze.com 2023-02-13 02:29 出处:网络
I have tables A and B and A_has_B where have this fields: A: id, name B: id, name A_has_B: a_id, b_id, background

I have tables A and B and A_has_B where have this fields:

A: id, name

B: id, name

A_has_B: a_id, b_id, background

Previously when I had not 'background' field in A_has_B, I did update by delete all record in A_has_B a开发者_开发技巧nd reinsert updated records.

but now how can I update A_has_B whitout deleteing records that should reinsert?


Off the top of my head, I would think something like....

DELETE FROM a_has_b
WHERE NOT EXISTS (
  SELECT 1 FROM b
  WHERE b.id=a_has_b.b_id
);

(and similar for table 'a')

or

DELETE FROM a_has_b
WHERE b.id <> ANY (SELECT id FROM b);


It sounds like you just need to

update A_has_B
set background = ???
where a_id = ??? and b_id = ???

You'll have to fill in the missing bits.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号