Previous: [MySQL must know and know (1)] [Official database table and SQL script import generation]
Article Directory
One, retrieve data
Use the SELECT statement to retrieve one or more data columns from the table
1.1 SELECT statement
To use SELECT to retrieve table data, you must give at least two pieces of information, what you want to choose and where to choose.
1.2 Retrieve a single column
mysql> SELECT prod_name
-> FROM products;
The above statement uses the SELECT statement to retrieve a column named prod_name from the products table. The required column names are given after the SELECT keyword, and the FROM keyword indicates the table command from which to retrieve data
1.2.1 Unsorted data
You will find that if you try this query several times, you will find that the order of the displayed data is different. If the query result is not clearly sorted, the order of the returned data has no special meaning. The order of returned data is random. As long as the number of returned rows is the same, it is normal.
1.2.2 End SQL statement
Multiple SQL statements must be separated by semicolons. MySQL, like most DBMSs, does not require a semicolon after a single SQL statement. However, a particular DBMS may have to add a semicolon after a single SQL statement. If you are using the mysql command line, you must add a semicolon to end the SQL statement.
1.2.3 SQL statements and capitalization
SQL statements are not case-sensitive. Many SQL developers like to use uppercase for all SQL keywords and lowercase for all column and table names, which makes the code easier to read and debug.
1.3 Retrieve multiple columns
To retrieve multiple columns from a table, use the same SELECT statement. The only difference is that multiple column names must be given after the SELECT keyword, and the column names must be separated by commas.
1.3.1 Comma
When selecting multiple columns, be sure to add a comma between the column names, but not after the last column name. If you add a comma after the last column name, an error will occur.
mysql> SELECT prod_id, prod_name, prod_price
-> FROM products;
1.4 Retrieve all columns
By using the * wildcard in place of the actual column name
mysql> SELECT *
-> FROM products;
1.4.1 Using wildcards
Reduce the use of * wildcards, but retrieving unnecessary columns will generally reduce retrieval and application performance.
1.5 Retrieving different rows
Use the DISTINCT keyword to return only different values
mysql> SELECT DISTINCT vend_id
-> FROM products;
1.6 Limit results
Return the first row or the first few rows, you can use the LIMIT clause
mysql> SELECT prod_name
-> FROM products
-> LIMIT 5;
LIMIT 5 instructs MySQL to return no more than 5 rows
mysql> SELECT prod_name
-> FROM products
-> LIMIT 5,5;
LIMIT 5,5 instructs MySQL to return 5 rows starting from row 5, the first number is the starting position, and the second number is the number of rows retrieved
1.6.1 Row 0
The first row retrieved is row 0 instead of row 1. LIMIT 1, 1 will retrieve the second row instead of the first row
1.7 Use fully qualified table names
mysql> SELECT products.prod_name
-> FROM products;
Fully qualified column name
Second, sort and retrieve data
2.1 Sort data
Return a single column of a database table, in no particular order
mysql> SELECT prod_name
-> FROM products;
mysql> SELECT prod_name
-> FROM products
-> ORDER BY prod_name;
Sort data alphabetically
2.2 Sort by multiple columns
To sort multiple columns, just specify the column name and separate the column names with commas
mysql> SELECT prod_id, prod_price, prod_name
-> FROM products
-> ORDER BY prod_price, prod_name;
2.3 Specify the sort direction
The default is ascending (from A to Z), descending DESC keywords
mysql> SELECT prod_id, prod_price, prod_name
-> FROM products
-> ORDER BY prod_price DESC;
mysql> SELECT prod_id, prod_price, prod_name
-> FROM products
-> ORDER BY prod_price DESC, prod_name;
The DESC keyword only applies to the column name directly before it.
2.3.1 Sort in descending order on multiple columns
If you want to sort in descending order on multiple columns, you must specify the DESC keyword for each column
2.3.2 Position of ORDER BY clause
When an ORDER BY clause is given, it should be ensured that it is located after the FROM clause. If LIMIT is used, it must be after ORDER BY. The wrong order of using clauses will result in an error message.