如何从两个表中选择记录并将其与其他字段一起插入mysql中的一个查询中的一个表中? [英] How to select records from two tables and insert it along with other fields in one table in one query in mysql ?
本文介绍了如何从两个表中选择记录并将其与其他字段一起插入mysql中的一个查询中的一个表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两张桌子 tbl_std(std_id,std_name,section_name)
& tbl_division(div_id,div_name,std_id,)
和 tbl_third(std_id,div_id,field_1,field_2
)。
现在我想从 tbl_std
中选择 std_id
和 div_id
来自 tbl_div
并将这些记录插入第三个表 tbl_third
。
我尝试使用内连接但未能插入数据。
我使用的是mysql数据库。
请帮忙。
I have two tables tbl_std(std_id,std_name,section_name)
& tbl_division(div_id,div_name,std_id,)
and tbl_third(std_id,div_id,field_1,field_2
).
Now I want to select std_id
from tbl_std
and div_id
from tbl_div
and insert these records into third table tbl_third
.
I have tried using inner joins but failed to insert the data.
I am using mysql database.
Please help.
推荐答案
试试这样,但不知道你到底在哪里。
Try like this, but don't know where you have struck.
insert into tbl_third(std_id,div_id,field_1,field_2)
select a.div_id,b.std_id,'1' as Field1,'2' as Field2 from tbl_division as a inner join tbl_std as b on a.std_id=b.std_id
这篇关于如何从两个表中选择记录并将其与其他字段一起插入mysql中的一个查询中的一个表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文