Mysql's question about one-to-many data updates

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.