about the except in SQL

Source: Internet
Author: User

1  Usetest12 SelectAddress_district,COUNT(address_district) asTotal fromHr_personal_testGroup  byAddress_district withRollup;3 Create TableTest1 (IDint);4 Create TableTest2 (numint);5 6 --1 1 (2) (1,4)7 --INSERT INTO TEST1 Select 1 Union SELECT 2 Union SELECT 38 --INSERT INTO TEST2 Select 1 Union Select 49 Ten --Select ID from test1 except select num from test2;-------return result 2,3 One  A  - Delete  fromtest1; - Delete  fromtest2; the --2 1 (2) (1,2,3,4) - --INSERT INTO TEST1 Select 1 Union select 2 - --INSERT INTO TEST2 Select 1 Union SELECT 2 Union SELECT 3 Union Select 4 - --Select ID from test1 except select num from test2;--------return result no +  - Delete  fromtest1; + Delete  fromtest2; A --3 1 (2) (3,4) at Insert  intoTest1Select 1 Union Select 2  - Insert  intoTest2Select 3 Union Select 4 - SelectId fromTest1except SelectNum fromTest2--------Return Results -  - Delete  fromtest1; - Delete  fromtest2; in --4 1 (2 ) - Insert  intoTest1Select 1 Union Select 2 Union Select 3 to Insert  intoTest2Select 1 Union Select 2  + SelectId fromTest1except SelectNum fromTest2--------Return Results 3

Create TableTest1 (IDint, namenvarchar( -))Create TableTest2 (IDint, postnvarchar(4))Insert  intoTest1 (name)Values('a'),('b'),('b'),('C')Insert  intoTest2 (POST)Values('a')SelectName fromTest1except SelectPost fromtest2--------back to B,cSelectName fromTest1whereName not inch(SelectPost fromTEST2)---------return to B,b,c

Thus, the following points can be drawn:

1. The except operator compares the data, not the fields with the same field names in different tables.

2. Except operator in a nutshell: Find data in the left table that is not present in the right table. Use a set to represent a a-a B rather than (A and B)-(A-B).

3, the except operator is going to be heavy, not in.

about the except in SQL

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.