Home  >  Article  >  Backend Development  >  求sql话语,根据两张表的相同字段插入到第三张表

求sql话语,根据两张表的相同字段插入到第三张表

WBOY
WBOYOriginal
2016-06-13 13:18:09973browse

求sql语句,根据两张表的相同字段插入到第三张表
我现在有两张表A 和 B 
现在A表里有个字段是:zhanghao,B字段里有个字段也是:zhanghao,
我现在想把A表里的数据和B表里是数据根据A表和B表相同字段相等,插入到第三张表中,怎么写SQL语句,用的mysql数据库,
谢谢大家了。

------解决方案--------------------
insert into test_C(
select * from test_A where test_A.zhanghao in(select test_b.zhanghao from test_B)
)
------解决方案--------------------
INSERT INTO C SELECT * FROM A;
INSERT INTO C SELECT * FROM B INNER JOIN A ON A.zhanghao = B.zhanghao;
------解决方案--------------------
insert into c
select * from a,b where a.zhanghao=b.zhanghao

*根据你自己的需求改成对应的字段
------解决方案--------------------
insert into test_c(zhanghao)(
select zhanghao from test_a where test_a.zhanghao in(select test_b.zhanghao from test_b)
)
第一次回答是三个表相同才能那样插入
对于不同字段不同,只对一个字段进行比较操作,用这个,只比对插入zhanghao字段
test_a为你说的A表
------解决方案--------------------
INSERT INTO C SELECT * FROM A,B where A.zhanghao = B.zhanghao;

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn