简体   繁体   English

将关联值从一个表存储到mysql中的另一个表

[英]Store associated value from one table to another table in mysql

Table one 表一

Id       subject       class     teacher
1        English         1          1
2        Math            1          2
3        SST             1          3
4        Computer        1          1
5        Physics         1          3
6        Chemistry       1          3

Each subject assigned to particular teacher, some subject has same teacher. 每个科目分配给特定的老师,某些科目具有相同的老师。

Now i want to store values in another table having these fields and other fields as well. 现在,我想将值存储在具有这些字段和其他字段的另一个表中。
Table Two 表二

id       subject        teacher

Whereas form only post subject ids, how can i get teacher id against each subject id from Table one and store it in table two? 表格仅发布科目ID,我如何从表一中获取每个科目ID的教师ID并将其存储在表二中?

Your question is very unclear. 您的问题还不清楚。 Are you asking for how to use relational databases and foreign keys? 您是否在要求如何使用关系数据库和外键? If so I would read up on how a relational database works and that will answer your question. 如果是这样,我将阅读有关关系数据库的工作原理,这将回答您的问题。 If the question is about how to do this in code then please state that and also post your attempt at it. 如果问题是关于如何在代码中执行此操作,请说明并发布您的尝试。

What i think you want to do is get the of the subject posted, ie query the database : 我想您想要做的是发布主题,即查询数据库:

SELECT subject, teacher FROM table_one WHERE id = 'POSTED_ID'  

Then insert the new record into the table two, something like : 然后将新记录插入表二,类似:

INSERT INTO table_two (subject, teacher) VALUES ('subject_value', 'teacher_value')   

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM