For example, there are two data tables, one is users and the other is user_metausers, which contains id, username, email, and other information user_meta, fields such as uid, meta_key, and meta_value are now classified as user A, such as HTML class... questions about one-to-many and data update
For example, there are two data tables: users and user_meta.
Users contains information such as id, username, and email.
User_meta contains fields such as id, uid, meta_key, and meta_value.
Now, if user A has A classification, such as HTML courses, PHP courses, and JS courses, you need to store the user_meta table in the following format:
Id, uid, meta_key, meta_value
1, 1, kemu, html
2, 1, kemu, php
3, 1, kemu, js
.......
If user A does not want to learn JS courses, update the fields with uid 1 and meta_value from the user_meta table at the front end. the other fields remain unchanged. of course, this is just to remove. if it is added, it is also a foreground interface operation.
Excuse me:
How to construct an SQL statement? Or is there a good solution to similar problems? Thank you for your solutions.
Reply content:
Questions about one-to-many and data update
For example, there are two data tables: users and user_meta.
Users contains information such as id, username, and email.
User_meta contains fields such as id, uid, meta_key, and meta_value.
Now, if user A has A classification, such as HTML courses, PHP courses, and JS courses, you need to store the user_meta table in the following format:
Id, uid, meta_key, meta_value
1, 1, kemu, html
2, 1, kemu, php
3, 1, kemu, js
.......
If user A does not want to learn JS courses, update the fields with uid 1 and meta_value from the user_meta table at the front end. the other fields remain unchanged. of course, this is just to remove. if it is added, it is also a foreground interface operation.
Excuse me:
How to construct an SQL statement? Or is there a good solution to similar problems? Thank you for your solutions.
Your answer is a good one.
Remember not to use the update statement. only insert and delete are used.
If you delete the expected Subject: delete from user_meta where uid = 1
If the user changes (or chooses for the first time) the expected subject, delete it first and then insert it. this encoding is the simplest and easier to understand.