mysql-third day

 

- SQL built-in functions to help document viewing
functions; see what type of function?

ex:
? round;


- grouping, the purpose of the group is to be polymerized statistics
- group by field
- query class student sex
select gender from students;

- see what kinds of sex
select distinct gender from students; - distinct deduplication

- the packet according to gender
select gender from students group by gender;

- calculate the number of each sex in the
select gender, count (*) from students group by gender;

- GROUP_CONCAT (...)
- inquiry packet data of each sex in the name
select gender, name from students group by gender; - an error, a corresponding results displayed only after gender group name in the
select gender , group_concat (name) from students group by gender; - group_concat (name) represents the name connected together
select gender, group_concat (name) as infos, count (*) from students group by gender;


Male Jay
Male Eddie
male Jacky Cheung
...
Female Shizuka
female Zhou Jie

Male: Jay, Eddie, Jacky
Female: Shizuka, Zhou Jie


- Query the same kind of name and gender in height
- Calculate the number of men
the SELECT COUNT (*) Students from the WHERE Gender = 1;
- is achieved by grouping
select gender, count (*) from students group by gender;

+ -------- + ---------- +
| Gender | COUNT (*) |
+ -------- + ---------- +
| M | 5 |
| F | 7 |
| neutral | 1 |
| Confidential | 1 |
+ -------- + ---------- +
needs to be done after the data in the packet further screening procedure
- use the having filter conditions have been represented for the data packet further screening
- further filtering operations on the data after the packet can only be done with screening without having to use WHERE
SELECT Gender, COUNT (* ) from Group by Students. Gender Gender = HAVING. 1;
SELECT Gender, COUNT (*) from Group by Students. Gender WHERE Gender =. 1; error

- In addition to the number of packets than boys
the SELECT Gender, COUNT (*) from the HAVING Students Group by Gender Gender = 1;!
The SELECT Gender, COUNT (*) from the HAVING Students Group by Gender Gender not = 1;

- having the data packet after further screening
- the average age of each sex query in AVG (Age)
the SELECT Gender, AVG (Age) Students from Group by Gender;

- The average age of the query avg (age) of each sex, maximum age, average height, maximum height, packet statistics in order to better
select gender, avg (age), max (age), avg (height), max (height) from students group by gender ;

- Query average age of over 30 years of age sex and the name, after the data packets for further screening operation the HAVING
the SELECT Gender, GROUP_CONCAT (name), AVG (Age) Students Group by Gender from the HAVING AVG (Age)> 30;
the SELECT gender, GROUP_CONCAT (name), AVG (Age) Students Group by gender from the HAVING AVG (Age)> 30;
- queries older than the average age of sex, name of data packets for further screening operation
select gender, name, age from students where age> (select avg (age) from students); ****


- having and where the difference between
having a packet of data has been expressed for further screening, there must be a group by having it, there is not necessarily a group by having
where is the source data to do the filtering operation

Gender SELECT, SUBSTRING_INDEX (GROUP_CONCAT (height by height desc Order), ',', 2) from Group by Students. Gender;
SUBSTRING_INDEX (STR, the delim, COUNT)
STR: the string to be processed
delim: Separator
count: Count

- Paging
- limit start, COUNT
- start: inquiries indicate where to start, start the default value is 0, can be omitted, the number of data start to skip
- count: How many queries

Obtaining a first page, 4 page display data
select * from students limit 0,4;

page 1

page 2

Page 3

Page 4


- 4 per page, displaying information on page 3, by age from small to large
the SELECT name from Student Age limit 8, 4 by the Order;
the SELECT name from the Order Student Age limit by 8, 4;


- connecting two query tables are merged together according to certain conditions

- Query student's name and student corresponding class name
- the name of the student in the student table, the class name in the class list
select students.name, classes.name from students, classes ; - Cartesian product queries may produce large middle watch
the SELECT * from Students, classes;
- Cartesian product query will produce a lot of useless information


- connection query data in accordance with two tables were screened connection condition setting, data to be able to meet the connection conditions being screened
- table1 inner join table2 on conditions, conditions in the connecting join query set
select students.name, Students. Inner classes from the Join classes.name ON students.cls_id = classes.id;
SELECT s.name, c.NAME from the Join Students. Inner classes AS AS S C = ON s.cls_id c.ID;
SELECT * S, C.. * from students as s inner join classes as c on s.cls_id = c.id;

- as required to display the name, the students and the corresponding class name (where the students in the class)
SELECT s.name, c.NAME from the Join Students. Inner classes AS AS S C = ON s.cls_id c.ID;
- at least query, the class name is displayed in column 1
oN s.cls_id = c.ID the SELECT c.name, s.name AS S students from the Join Inner classes AS c;
- classes where students query, sorted according to class
select c.name, s.name from students as s inner join classes as c on s.cls_id = c.id order by c.name;


- outer join query: left + right the Join the Join
- left the Join a left outer join query
- Query class information corresponding to each student, the data connection does not meet the conditions will be NULL fill
select s *, c * from students .. as s left join classes as c on s.cls_id = c.id;

- right join right outer join query using relatively small
- the name of the data table are reversed, with the left join to complete

- expansion of understanding, other formulations within the connector and the outer connector
- connecting the other formulations within the
SELECT S *, C * from the Join Students. Inner classes AS AS S C = ON s.cls_id c.ID;..
SELECT * S. , C * from the Join Students. S AS AS classes C = ON s.cls_id c.ID;.
.. S SELECT *, C * from the Join classes Students. Cross AS AS S C = ON s.cls_id c.ID;

-- 外连接的其他写法
select s.*,c.* from students as s left outer join classes as c on s.cls_id = c.id;
select s.*,c.* from students as s right outer join classes as c on s.cls_id = c.id;

Guess you like

Origin www.cnblogs.com/zxw2016208120/p/11358435.html