MySQL intersection difference set union

Source: Internet
Author: User

I. MySQL

Only the Union set does not have the keyword of the intersection difference set

Two-Table Structure

A B

Three-Union

3.1 union does not contain duplicate data

 
SelectNameFromAUnionSelectNameFromB;

 

3.2 Union all contains duplicate data

Four difference set

Find the ID that exists in Table A but does not exist in Table B

4.1: use Union

 Select ID From  ( --  Union  Select   Distinct A. ID As ID From  A  Union   All  Select   Distinct B. ID As ID From  B)  Temp   Group  By ID Having   Count (ID) =   1 ;

 

4.2:Subquery not in

 
SelectIDFromAWhereIDNot In(SelectIDFromB );

4.3:Subquery not exists

SelectIDFromAWhereNot Exists(SelectIDFromBWhereA. ID=B. ID );

4.4: left join determines that the right table is null

 
SelectA. IDFromALeft JoinBOnA. ID=B. IDWhereB. IDIs Null Order ByA. ID

 

Five intersection intersect:

 Select ID From  (  --  Union  Select   Distinct A. ID As ID From  A  Union   All  Select   Distinct B. ID As IDFrom  B)  Temp   Group   By ID Having   Count (ID) ! =   1 ;

 

 

Related Article

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.