17 Combination Query

Source: Internet
Author: User

There are multiple SELECT statements, and multiple query results are returned as a single query result, and these combined queries are called and or compound queries
Usage: Returns similar structure data from different tables in a single query
Execute multiple queries against a single table, returning data as a single query
1 Create a combined query: Use the union operator to combine several SQL queries, and use union to give more than one select
Statements that combine their results into a single result set,

SELECT Id,name,price from Products WHERE Price <=5;

SELECT Id,name,price from Products WHERE ID in (1001,1002);
Select Id,name,price from Products where price <=5
UNION
SELECT id,name,price from products where ID in (1001,10 02);

Or

SELECT Id,name,price from the products WHERE the price <=5 OR ID in (1001,1002);

Rule: Each query must contain the same columns, expressions, or aggregate functions
The default repeating row is automatically canceled, and you want to return all matching rows, including the duplicate union ALL

2 sort, can only have one order by and not allow part one to sort another part by one sort

Select Id,name,price from Products where price <=5
UNION
SELECT id,name,price from products where ID in (1001,10 )
ORDER by Id,price;

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.