How to Use the order by clause of MySQL learning notes for beginners

Source: Internet
Author: User

Last time we introduced: Suitable for beginnersMySQLFor more information about the usage of SELECT statements for learning notes, we will introduce some suitable MySQL learning notes for beginners.Order by clauseNext, let's take a look at this part.

Sort results by order by clause

The order by clause sorts the query results BY one or more up to 16 fields, which can be ASC (Ascending ORDER) or DESC (descending ORDER). The default value is ascending. The ORDER clause is usually placed at the end of an SQL statement. Multiple fields are defined in the ORDER clause.

Example:

 
 
  1. SELECT ProductName,UnitPrice, UnitInStock   
  2. FROM Products   
  3. ORDER BY UnitInStock DESC , UnitPrice DESC, ProductName  

In the order by clause, you can replace the field name with the position number in the field selection list. You can mix the field name and position number.

For example, the following statement produces the same effect as the preceding one.

 
 
  1. SELECT ProductName,UnitPrice, UnitInStock   
  2. FROM Products   
  3. ORDER BY 1 DESC , 2 DESC,3 

Multi-Table query using connection relationships

Example: Find the names of suppliers and customers in the same city

 
 
  1. SELECT Customers.CompanyName, Suppliers.ComPany.Name   
  2. FROM Customers, Suppliers   
  3. WHERE Customers.City=Suppliers.City  

For example, find out the products and orders with the product inventory greater than the order quantity of the same product.

 
 
  1. SELECT ProductName,OrderID, UnitInStock, Quantity   
  2. FROM Products, [Order Deails]   
  3. WHERE Product.productID=[Order Details].ProductID   
  4. AND UnitsInStock>Quantity  

Another method is to use the jnner join exclusive to Microsof jet SQL.

Syntax:

 
 
  1. FROM table1 INNER JOIN table2   
  2. ON table1.field1 comparision table2.field2  

Comparision is the comparison operator used by the WHERE clause.

 
 
  1. SELECT FirstName,lastName,OrderID,CustomerID,OrderDate   
  2. FROM Employees   
  3. INNER JOIN Orders ON Employees.EmployeeID=Orders.EmployeeID 

Note:
Inner join cannot be used to connect Memo OLE Object Single Double data type fields.

JOIN multiple ON clauses in a JOIN statement. Syntax:

 
 
  1. SELECT fields   
  2. FROM table1 INNER JOIN table2   
  3. ON table1.field1 compopr table2.field1 AND   
  4. ON table1.field2 compopr table2.field2 OR   
  5. ON table1.field3 compopr table2.field3  

Yes.

 
 
  1. SELECT fields   
  2. FROM table1 INNER JOIN   
  3. table2 INNER JOIN [( ]table3   
  4. [INNER JOER] [( ]tablex[INNER JOIN]   
  5. ON table1.field1 compopr table2.field1   
  6. ON table1.field2 compopr table2.field2   
  7. ON table1.field3 compopr table2.field3  

External connections return more records and keep unmatched records in the results. No matter there are no records that meet the conditions, all records on the other side are returned.

 
 
  1. FROM table [LEFT|RIGHT]JOIN table2   
  2. ON table1.field1comparision table.field2 

Use the left join to create an external join. All the data in the table on the left of the expression is displayed.

For example, all products are returned regardless of the order quantity.

 
 
  1. SELECT ProductName ,OrderID   
  2. FROM Products   
  3. LEFT JOIN Orders ON Products.PrductsID=Orders.ProductID  

The difference between the right join and the left join is that, no matter whether there are matching records in the left table, it returns all records from the left table.

For example, if you want to know the customer information and calculate the customer distribution in each region, you can use a right connection to return the customer information even if there are no customers in a region.
NULL values do not match each other. You can use an external connection to test whether the fields in a joined table have null values.

 
 
  1. SELECT *   
  2. FROM talbe1   
  3. LEFT JOIN table2 ON table1.a=table2.c  

Grouping and summarizing query results

In SQL syntax, the GROUP BY and HAVING clauses are used to summarize data.Group by clauseSpecifies which fields are grouped. After grouping records, use the HAVING clause to filter these records.

Syntax of the group by clause:

 
 
  1. SELECT fidldlist   
  2. FROM table   
  3. WHERE criteria   
  4. [GROUP BY groupfieldlist [HAVING groupcriteria]]  

Note: Microsoft Jet Database cannot group remarks or OLE object fields. The Null Value in the group by field is used for grouping, but cannot be ignored. No Null value is calculated in any SQL aggregate function. A group by clause can contain up to ten fields, and the sorting priority is arranged from left to right.

For example, After grouping by title in the employee table of the 'wa 'region, find all titles with the same title with more than 1 employee.

 
 
  1. SELECT Title ,Count(Title) as Total   
  2. FROM Employees   
  3. WHERE Region = ‘WA’   
  4. GROUP BY Title   
  5. HAVING Count(Title)>1 

This article introduces how to use the order by clause of MySQL learning notes for beginners. I hope this article will be helpful to you!

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.