Mysql full join query merge Fields

Source: Internet
Author: User
1. Create two tables t1 as follows: idname1a13a3t2: idname1b12b25b5 Combined Query merging field: SQL statement: SELECTt3. 'name' ASB, t3. 'name1' ASC, COALESCE (t3.id2, t3.id1) ASmyIdFROM (SELECTt1.idasid2, t1. 'name' asname, t2.idasid1, t2. 'Na

1. Create two tables t1 as follows: id name 1 a1 3 a3 t2: id name 1 b1 2 b2 5 b5 Combined Query merge field: SQL statement: SELECT t3. 'name' as B, t3. 'name1' AS C, COALESCE (t3.id2, t3.id1) AS myId FROM (SELECT t1.id as id2, t1. 'name' as name, t2.id as id1, t2. 'Na

1. Create two tables

T1 is as follows:

Id Name
1 A1
3 A3


T2 is as follows:

Id Name
1 B1
2 B2
5 B5


Combined Query merge fields:

SQL statement:

SELECT t3. 'name' as B, t3. 'name1' AS C, COALESCE (t3.id2, t3.id1) AS myId

FROM (SELECT t1.id as id2, t1. 'name' as name, t2.id as id1, t2. 'name' as name1 FROM t1 left join t2 on t1.id = t2.id

Union select t1.id as id2, t1. 'name' as name, t2.id as id1, t2. 'name' as name1 from t1 right join t2 on t1.id = t2.id) as t3 group by myId;

Query results:


B C MyID
A1 B1 1
B2 2
A3 3
B5 5

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.