1. The underlying query
(1) query the whole table
The SELECT * the FROM table name; - not recommended "*", is not convenient to read
(2) selective polling
SELECT column names, column names, column names FROM table;
(3) to re-query
The SELECT DISTINCT column name FROM table;
To re-query (4) with multi-column names
The SELECT DISTINCT column name, column name FROM table name; / * and single name to re-query the difference,
to meet more column names combining a non-exclusive nature can be considered repeat, repeat considered two exactly the same results * /
(5) calculating the numerical data
SELECT column name, column name, column name + column names FROM table; - Note, NULL add any value to NULL. Value can take more than arithmetic.
(6) with the correction data calculated
SELECT column names, column names, column names + IFNULL (column name, data specified) the FROM table; / * if the specified column is NULL,
then the specified data set to NULL * /
(7) from a column name alias
SELECT column names the AS ' alias ' column names the AS ' alias ' the FROM table name; - the AS can be replaced with spaces
2. Conditions inquiry
(1) Compare
SELECT column names FROM table name WHERE column name > condition value; - query condition value is greater than the data
SELECT column names FROM table name WHERE column name < condition value; - query condition value is smaller than the data
SELECT column names FROM table name WHERE column name > = condition value; - query data value greater than or equal conditions
SELECT column names FROM table name WHERE column name <= condition value; - query data condition value less than or equal
SELECT column names FROM table name WHERE column name = condition value ;- query data value equal conditions
SELECT column names FROM table name WHERE column name <> condition value; - query conditions is not equal to the data value
SELECT column names FROM table name WHERE column name =! Condition value; - Query is not equal to data condition value, MySQL-specific.
(2. area
SELECT column names FROM table name WHERE column names BETWEEN value 1 the AND condition value 2; / *
query or greater condition value of 1 to less than or equal condition value data 2 * /
SELECT column names FROM table name WHERE column names the IN (value 1 , value 2); / *
condition value matches the query column names and set data * /
(3) a null value
SELECT column names FROM table name WHERE column names the IS NULL ; / * query is specified as NULL data.
NULL is very special, the operator can not be combined. * /
The SELECT column names FROM table WHERE column name IS the NOT NULL ; - query specifies the column is not NULL data.
(4) logical operators
SELECT column names FROM table name WHERE column name = condition value AND column name = condition value; - at the same time is a value that meets all the conditions of the query
SELECT column names FROM table name WHERE column name = condition value OR column name = condition value; - value matches any of the conditions are the query
SELECT column names FROM table name WHERE column name NOT the iN (value 1, value 2); / *
query condition values in addition to any data behind any conditions negated NOT * /
(5) fuzzy query
SELECT column names FROM table name WHERE column names the LIKE ' condition value% ' ; - similar to the wildcard, "%" stands for any number of characters
SELECT column names FROM table name WHERE column names the LIKE ' condition value _ ' ; / * similar wildcard, "_" represents any number of characters,
the two mixing arrangement may be placed anywhere * /
3. Sort inquiry
(1) Sort basis
SELECT column names FROM table name the ORDER BY column name;
(2) specified in the order
SELECT column names FROM table the ORDER BY column name sort, sort column names; / *
combination sort column names in front of the high-priority ordering, under the same conditions, a determination of priority of data * /
SELECT column names FROM table the ORDER BY column names the ASC ; - ascending order, and default sort
SELECT column names FROM table the ORDER BY column name DESC ; - descending
4. aggregate function
The polymerization is to function as a whole longitudinal data calculation, shows the results.
But the calculation is to exclude NULL null value, try not to calculate the column contains a null value, the result will lead to the expected deviation.
(1) ignore the null value
The SELECT COUNT (column names) the FROM table; - calculating a number of non-null data in
the SELECT MAX (column names) the FROM table; - calculating a maximum non-null data in
the SELECT MIN (column names) the FROM table name; - calculating the minimum value of a non-null data in
the SELECT the sUM (column names) the FROM table; - calculation of the number of a non-null data and
the SELECT the AVG (column names) the FROM table; - calculating a number of non-null data average value
(2) Processing null
The SELECT COUNT (the IFNULL (column name, substitute value)) the FROM ; table - counting the number of data of a
5. grouping queries
Same column packet content query is the principle of the specified group, and the specified data packet polymerization calculation.
Data to be queried must have in common, or check out the non-common data is meaningless.
(1) Normal packets
SELECT column name, aggregate functions (column names) the FROM table the GROUP BY grouping column name;
(2) limits the packet
With the data packet defined, using WHERE and HAVING results before and after treatment.
SELECT column name, aggregate functions (column names) the FROM table name WHERE column name value operator conditions
the GROUP BY grouping column names HAVING column name / aggregate function (column names) operator condition value;
The WHERE clause is processed before the data packet; the HAVING clause is a post-processing of the result packet processing.
Because the WHERE raw data of the packet, it can not be a function of the polymerization process; HAVING the two data can be processed.
6. paging query
To check out the data into multiple displays, specify the start of each set of data and index page shows how much data is complete.
Index is calculated per start: = start index page (current page number - 1) * the number of per page.
The first page index is zero, the page index is started in accordance with the number of query data row.
Note: LIMIT clause is a MySQL-specific statements, other database software have different pagination statement.
SELECT column names FROM table LIMIT starting index page, each page a few data;