Excerpt from rookie tutorial, part of the knowledge point record according to my own situation. In order to consolidate a knowledge learned, and secondly for future review.
Mysql base notes three
Where clause
To conditionally select data from the table, can be added to the WHERE clause of a SELECT statement.
where passenger for select, delete, update command.
BINARY keyword may be used to set the where clause string is case-sensitive.
LIKE clause
Like clause any character identification unsolicited% sign, percent sign% if not used, and the = Like clauses same effect.
Statement:
SELECT Field 1, Field 2, ..., n from the field table where field m Like condition;
- '% A': a data ending
- 'A%': beginning a data
- '% A%': contains a data
- ' A ': is a three-bit and intermediate data
- '_A': two and ending with a data
- 'A_': two data and starts with a
Represents any of 0% or more characters, the character can match any type and length
_ represents any single character
[] represents a character listed in the brackets
[^] represents a single character is no longer in a plurality of rows of brackets
Note : when the query contains wildcards (more symbols), the special characters "[]" can be enclosed in
UNION operator
The results for the select statement is connected to two or more combinations of a set of results. Multiple select statement removes duplicate data.
grammar:
select a,b,c from tb1 [where condition] UNION [ALL/DISTINCT] select a,b,c from tb2 [where condiction]
all: return all result sets that contain duplicate data
distinct: the result set delete duplicate data
tips: UNION default out duplicate data.
Sort Mysql
Sort order by using the
syntax:
select 字段1,字段2,...,字段n from table
order by 字段m [ASC/DESC] ,字段p [ASC/DESC],...
ASC ascending order, DESC descending, descending default.
tips:
alphabetical order:
- If the character set gbk, directly order by ordering
- If the character set utf-8, the first character transcoding reorder
select * from table order by convert (field n using gbk)
Group by statement
They are grouped according to one or more queue result sets.
Using COUNT, SUM, AVG, etc. functions on the packet column.
grammar:
SELECT column_name, function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name;
WITH ROLLUP can be achieved then the same statistics (SUM, AVG, COUNT ...) on the basis of statistical data in the packet.
Tips:
the Where, Group by, the HAVING difference:
- where: Inst for screening query results and work before returning
- Group by: to select query result set out in accordance with a field or expression group
- For and where to check out the packet group by filtration, the isolated grouping result satisfaction condition, the query results filtering operation after the return result sets.
Execution order:
select-> where-> by-Group> having-> Order by
where 和 having:
①select s#,sage from Student where sage>15;
②select s#,sage fron Student having sage>15;
③select s# from Student having sage>15;
- where screening directly from the data field in a table
- re having screened out from the screening field, it will be given first sentence ③
- ①② statement, where and sage equivalent