SQL Getting Started collection operations

Source: Internet
Author: User
Tags mysql version

Although you can only process one row of data at a time while interacting with a database, the relational database typically handles a collection of data. The commonly used set operations in mathematics are: and (Union), intersection (intersect), poor (except). The following two requirements must be met for a set operation:

    • Two data sets must have the same number of columns
    • The data type of the corresponding column in a data set must be the same (or the server can convert one of these types to another)

Each set operator in the SQL language contains two adornments: one containing duplicates, the other removing duplicates (but not necessarily eliminating all duplicates).

0.union operator

The union and Union All operators can concatenate multiple datasets, except that the former removes duplicates and the latter remains. Theunion All operation is more rapid for the database because the database does not need to check the repeatability of the data. For even a table

Select ' IND ' type_cd, cust_id, lname name  from individual Union  All Select ' BUS ' type_cd, cust_id, name  from business;

The result after performing the operation is:

Of these, 18 data came from the individual table and 8 from business. To see the difference more clearly, look at the following two examples:

Select a.cust_id, A.name  from  allselect  b.cust_id, b.name from business b;

Result is

and

Select a.cust_id, A.name  from Business a Union Select b.cust_id, B.name  from business b;

Result is

The difference can be clearly seen.

1.intersect and except Operators

The MySQL version I used does not seem to implement these two features. :(

SQL Getting Started collection operations

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.