SQL statement. insert the statement to the third table based on the same fields of the two tables.

Source: Internet
Author: User
Evaluate the SQL statement and insert it to the third table based on the same fields of the two tables. I now have two tables A and B & nbsp; now there is A field in Table A: zhanghao, there is also A field in field B: zhanghao. I want to insert the data in Table A and Table B into the third table based on the same fields in Table A and Table B, how to write SQL statements, using the mysql database, thank you for your SQL statement, insert the same fields of the two tables to the third table
I have two tables, A and B.
Now there is A field in Table A: zhanghao, and field B also has A field: zhanghao,
I want to insert the data in Table A and Table B into the third table based on the same fields in Table A and Table B. how to write SQL statements and use the mysql database,
Thank you.

------ Solution --------------------
Insert into test_C (
Select * from test_A where test_A.zhanghao in (select test_ B .zhanghao from test_ B)
)
------ Solution --------------------
Insert into c select * from;
Insert into c select * from B inner join a on a. zhanghao = B. zhanghao;
------ Solution --------------------
Insert into c
Select * from a, B where a. zhanghao = B. zhanghao

* Change the field as needed.
------ Solution --------------------
Insert into test_c (zhanghao )(
Select zhanghao from test_a where test_a.zhanghao in (select test_ B .zhanghao from test_ B)
)
The first answer is that three tables can be inserted in the same way.
Only one field is compared for different fields.
Test_a refers to table A for you.
------ Solution --------------------
Insert into c select * from a, B where A. zhanghao = B. zhanghao;

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.