I do not know how to write SQL statements.

Source: Internet
Author: User
I do not know how to write SQL statements for help. I have two tables, table A and Table B, with the following fields: zhanghao, mobile, and Table B: zhanghao, username, and address. Currently, table A has 15 data records in total, table B has 20 data records. I want to insert the same field values of Table A and Table B to the third table, insert the excess five pieces of data into the third table for help.
I have two tables, table A and Table B, with the following fields: zhanghao, mobile, and Table B: zhanghao, username, and address. Currently, table A has 15 data records in total, table B has 20 pieces of data. I want to insert the same field values of Table A and Table B into the third table, and insert the excess five pieces of data into the third table, that is, insert data to a data record equally, and insert data to a data record unevenly. how can this problem be solved? Thank you.

------ Solution --------------------
Use tables A and B to join tables with the same account. insert tables C with different inserted tables D.
SQL code
Insert into c (Field 1, field 2, Field 3) field 1, field 2, Field 3 from a, B where. zhanghao = B. zhanghao; insert into d (Field 1, field 2, Field 3) field 1, field 2, Field 3 from a, B where. zhanghao! = B. zhanghao;
------ Solution --------------------
Insert into C select * from A, B where A. zhanghao = B. zhanghao;

Insert into C select * from B where zhanghao not in (select zhanghao from );
------ Solution --------------------
I mean if the values of the same fields in Table A and Table B are the same, insert A piece of data to the third table. if the values are not the same, insert the data in table A to the third table.

I think it's awkward for you.
A
1 mobile1
2 mobile2
3 mobile3
B
1 zhang bj
2 li shang
4 wang ji

Is it true that, like zhanghao, A + B data is inserted into C, while B does not exist in A only inserts A into C ???
------ Solution --------------------
What if A and B do not?

Insert into C
Select * from A, B where A. zhanghao = B. zhanghao
Union
Select * from
Union
Select * from B



------ Solution --------------------
Therefore, the person who asks the question does not describe the question clearly and asks us to answer the question.
Ah ~~~~~
------ Solution --------------------
Discussion

What if A and B do not?

Insert into C
Select * from A, B where A. zhanghao = B. zhanghao
Union
Select * from
Union
Select * from B

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.