更新组中的用户列表:UPDATE 或 DELETE + INSERT [英] update user list in group: UPDATE or DELETE + INSERT
问题描述
第 8 组包含用户:1、2、3
Group 8 contains users: 1, 2, 3
user_id | group_id
------------------
1 | 8
------------------
2 | 8
------------------
3 | 8
------------------
需要更新第 8 组的用户列表.
Need to update user list for group 8.
所以第 8 组应该包含用户:1, 5, 7
So group 8 should contain users: 1, 5, 7
user_id | group_id
------------------
1 | 8
------------------
5 | 8
------------------
7 | 8
------------------
对于这种特殊情况和表结构,最好的方法是什么?
For this particular case and table structure, what is the best way to do it?
UPDATE"正在更新表中的现有记录.所以我想更新"对我不起作用,对吗?
"UPDATE" is updating an existing record(s) in a table. So I guess "UPDATE" will NOT work for me, right?
我是否必须删除(使用DELETE")组 8 中的所有用户并插入(使用INSERT INTO")一个新的更新用户列表?
Do I have to delete (with "DELETE") all users from group 8 and insert (with "INSERT INTO") a new updated list of users?
更新前的用户列表和更新后的用户列表是任意的和动态的,可能与我的示例不完全相同.
user list before update and user list after update is arbitrary and dynamic, may not be exactly like in my example.
推荐答案
是的,删除和插入是要走的路.您删除用户 2 和 3,然后添加用户 5 和 7.您不需要触摸用户 1.
Yes, delete and insert is the way to go. You remove user 2 and 3, then you add user 5 and 7. You do not need to touch user 1.
更新不好,因为从概念上讲,您正在从组中删除一个用户,而您又加入了另一个用户.您不会将一个用户转变为另一个用户.
An update is not good, because conceptually your are removing a user from the group and you join another one. You're not transforming one user into an other.
这篇关于更新组中的用户列表:UPDATE 或 DELETE + INSERT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!