Mysql database day2

Conditions inquiry

- Comparison Operators
- select .... from table ..... the WHERE
->
- Query more than 18-year-old information

select * from students where age > 18;

- <
- less than 18 years of information queries

select * from students where age < 18;

- =
- Query age of 18 years for all students by name

select * from students where age = 18;

-! = Or <>

select * from students where age != 18;
select * from students where age <> 18;

Logical Operators

- and
- between 2818 and all student information

select * from students where age>18 and age<28;

- 18-year-old woman

select * from students where age>18 and gender="女";

- or
- more, or 18 is higher than the height 180 (comprising) above

select * from students where age>18 or height>=180;

- not
information in the over 18 year-old woman is not in this range -

select * from students where not (age>18 and gender="女");

- Fuzzy query (data where name like to be queried)
- like
-% replace any number
- _ replacing one
- query name to "small" start name

select * from students where name like "小%";

- query name in the "small" all the names

select * from students where name like "%%";

- Query There are two words in the name

select * from students where name like "__";

- inquiries have names 3 words

select * from students where name like "___";

- query has at least two-word name

select * from students where name like "__%";

- range queries
- in (1, 3, 8 ) expressed in a non-continuous range
- the name of the query aged 18, 34

select * from students where age in (18, 34);

select * from students where age=18 or age=34;

- not in not a non-continuous range of
- age is not 18, 34-year-old information

select * from students where age not in (18, 34);

select * from students where not (age in (18, 34));

(注意)select name from students where not age in (18,34);

- between ... and ... mean within a continuous range
- information search between the ages of 18-34

select * from students where age between 18 and 34;
select * from students where age>=18 and age<=34;

- not between ... and ... express not in a continuous range
- query aged between 18-34 are not in the information

select * from students where age not between 18 and 34;

- Empty judgment
- judgment null null IS
- Height query information is empty

select * from students where height is null;

- sentenced to a non-null is not null

select * from students where height is not null;

 Sequence

- order by field
- asc ascending order, i.e. ascending
- desc descending order, i.e., in descending order

- Query male between the ages of 18-34 years old, according to age from small to large to sort (default is ascending asc)

select * from students where (age between 18 and 34) and (gender='');
    
select * from students where (age between 18 and 34) and (gender='') order by age;

 

- Query women between the ages of 18-34 years old, height from high to low sort

select * from students where (age between 18 and 34) and (gender='') order by height desc;

- order by multiple fields
- queries aged women between 18 and 34 years old, height from high to low sort, under the same circumstances if the height by age descending order

select * from students where (age between 18 and 34) and (gender='') order by height desc, age desc;

Women query aged between 18 to 34 years old, height from high to low sort, according to the descending order of age, under the same circumstances height -
- If age is the same as that descending order according to id

select * from students where (age between 18 and 34) and (gender='') order by height desc, age desc, id desc;

 

Aggregate function

- Total
- count
the total number of students in the class of computing -

select * from students;
select count(*) from students;

- Query male how many people, how many people are female

select count(*) from students where gender='';
select count(*) from students where gender='';

- maximum
- max
- the oldest inquiry

select max(age) from students;

- Query women's highest height

select max(height) from students where gender='';

- minimum value
- min

select min(age) from students;

- sum
- SUM
- sum calculated for all ages

select sum(age) from students;

- the average
- AVG
- The average age calculation

select avg(age) from students;

- The average age calculated sum (age) / count (*)

select sum(age)/count(*) from students;

- rounded round (123.23, 1) to retain a decimal
- the average age of computing for all, 2 decimal places

select round(avg(age), 2)  from students;

 

Packet (focus)

- Group by
- grouped by sex, sex all queries

select gender from students group by gender;

select distinct gender from students;

select name,gender from students group by name,gender;

select name,gender from students group by gender;错误

- calculate the number of each sex in

select gender,count(*) from students group by gender;

- Calculate the number of each age

select age,count(*) from students group by age;

- Query to sex in the maximum age

select gender,max(age) from students group by gender;

select gender,max(age) from students group by gender having gender in ('', '');

select gender,max(age) from students where gender in ('', '') group by gender;

- GROUP_CONCAT (...)
- query name in the same kind of sex

select gender,group_concat(name) from students group by gender;

- role with rollup summary

select gender,group_concat(name),max(age),max(height) from students group by gender with rollup;

 limit inquiry

- limit Start, COUNT

- check out the number of data limitations
- before data query 5

select * from students limit 0, 5;

-- Paging query:

select * from students limit (n-1)*m, m;

- 2 per page, the first page of a

select * from students limit 0, 2;

- 2 per page, the second page

select * from students limit 2, 2;

- per page 2, the first three pages

select * from students limit 4, 2;

- 2 per page, 4 pages

select * from students limit 6, 2;

 Join query (focus)

-- inner join ... on
-- select ... from 表A inner join 表B;

select * from students inner join classes;

- students can query the corresponding classes and class information

select * from students inner join classes on students.cls_id=classes.id;

- in accordance with the requirements of the display name, class

select students.name,classes.name from students inner join classes on students.cls_id=classes.id;

- to the data from the table name

the SELECT students.name AS student name, classes.name AS class name from Students Inner  the Join classes ON students.cls_id = classes.id;

- writing another within the connector

select * from students, classes where students.cls_id=classes.id;

- students can query the corresponding classes and class information, displays all the information students students * to display only the class name classes.name..

select classes.name,students.* from students inner join classes on students.cls_id=classes.id;

- Queries can have a corresponding class of student and class information, sorted according to the number of classes
- select c.xxx s.xxx from students as s inner join clssses as c on .... order by ....;

select classes.name,students.* from students inner join classes on students.cls_id=classes.id order by classes.id;

- at the time when the same class, from small to large order in accordance with student id

select classes.name,students.* from students inner join classes on students.cls_id=classes.id order by classes.id, students.id;

select c.name,s.* from students as s inner join classes as c on s.cls_id=c.id order by c.id, s.id;

select c.name,s.* from students s inner join classes c on s.cls_id=c.id order by c.id, s.id;

select c.name,s.* from students s, classes c where s.cls_id=c.id order by c.id, s.id;

 

- self-join queries
- and their own table to establish a connection

- 1, the number of provinces, a total of inquiry

select * from areas where pid is not null;

- 2, the name of the query Province as "Guangdong Province" of all cities

select * from areas as c inner join areas as p on c.pid=p.id where p.title='广东省';

--3, province name query for "Shenzhen" in all counties

select * from areas as a inner join areas as c on a.pid=c.id where c.title='深圳市';

- subquery
- scalar Query: subquery returns a result of data (one line)
- column subquery: returns a result (a multi-line)
- row subqueries: returned result is a row (line multi-column)

- query the information above average height (height)
- the average height of 1 found

select avg(age) from students;
select avg(height) from students;

- 2 information found higher than average height

select * from students where age > (select avg(age) from students);
select * from students where height > (select avg(height) from students);

- Query student school number and class number corresponding student information
- Students from the SELECT name in the WHERE cls_id (the SELECT id from classes);
- 1 to detect any class id

select id from classes;

- 2 can detect the corresponding student information on the class numbers (class number of queries of students, there is the class table, student information)

select * from students where cls_id in (select id from classes);

- Example 3. Find the oldest, highest student Height:

select max(age),max(height) from students;

select * from students where (age,height) = (select max(age),max(height) from students);

 

Guess you like

Origin www.cnblogs.com/tracydzf/p/12581563.html