T
T
TheInsable2017-06-27 21:04:39
Database
TheInsable, 2017-06-27 21:04:39

What is the best way to update data in a crosstab?

For example, there is a table users: id, name and a table groups: id, name (users can have many groups), they are connected by a cross-table users_groups: user_id, group_id.
What is the best way to update groups for a specific user (users_groups table)? For example, there is a user with id 1, he is included in 3 groups with id 5, 10, 20. And if I imagine that in some application I remove one group from this user and leave only groups with id 5 and 20, then what is the best just update the users_groups table?
I understand that the question may seem too simple, but only some bicycles appear in my head about this, I think that there is a very concise solution to this problem.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2017-06-27
@sim3x

user_groups

id | user_id | group_id
1  |     1   |    5
2  |     1   |    10
3  |     1   |    20

DELETE FROM user_groups WHERE user_id = 1 AND group_id = 10

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question