2. Database Query

- preparation of data
    - Creating a database
    create database python_test charset=utf8;
 
    - Use a database
    use python_test;
 
    - Displays current data using what?
    select database();
 
    - Create a data table
    -- students表
    create table students(
        id int unsigned primary key auto_increment not null,
        name varchar(20) default '',
        age tinyint unsigned default 0,   # tinyin 最小值
        height decimal (5,2), # decimal decimal
        gender enum ( 'male', 'female', 'neutral', 'secret') default 'confidential'
        cls_id int unsigned default 0,
        is_delete bit default 0
    );
 
    - classes table
    create table classes (
        id int unsigned auto_increment primary key not null,
        name varchar(30) not null
    );
 

Prepare data

- insert data insert into students values ​​to students table
(0, 'Hsiao Ming', 18,180.00,2,1,0),
(0, 'Satsuki', 18,180.00,2,2,1),
(0, 'Eddie', 29,185.00,1,1,0),
(0, 'Andy', 59,175.00,1,2,1),
(0, 'Huang Rong', 38,160.00,2,1,0),
(0, 'Feng', 28,150.00,4,2,1),
(0, 'WangZuXian', 18,172.00,2,1,1),
(0, 'Jay', 36, NULL, 1,1,0),
(0, '- Kun', 27,181.00,1,2,0),
(0, 'Crystal Liu', 25,166.00,2,2,0),
(0, 'Venus', 33,162.00,3,3,1),
(0, 'Shizuka', 12,180.00,2,4,0),
(0, 'Guo Jing', 12,170.00,1,4,0),
(0, 'Zhou Jie', 34,176.00,2,5,0);
 
- a table of data inserted into classes insert into classes values ​​(0, "python_01 period"), (0, "python_02 period");
 
 
-- Inquire
    - Query all fields
    -- select * from 表名;
    select * from students;
    select * from classes;
    select id, name from classes;
 
    - Query the specified field
    - select row 1, column 2, ... from table name;
    select name, age from students;
    
    - from the field to use as an alias
    - select field as the name .... from table name;
    select name as the name, age as the age from students;
 
    - select table fields .... from table name;.
    select students.name, students.age from students;
 
    
    - You can play through as an alias to the table
    . - select Alias ​​field .... from table name as an alias;
    select students.name, students.age from students;
    select s.name, s.age from students as s;
    - Failed select students.name, students.age from students as s;
 
 
    - Eliminate duplicate rows
    - distinct field
    select distinct gender from students;
 
 
- Conditions inquiry
    - Comparison Operators
        -- select .... from 表名 where .....
        -- >
        - Query more than 18-year-old information
        select * from students where age>18;
        select id,name,gender from students where age>18;
 
        -- <
        - less than 18 years of information queries
        select * from students where age<18;
 
        -- >=
        -- <=
        - less than or equal to 18 years old query information
 
        -- =
        - Query age of 18 years for all students by name
        select * from students where age=18;
 
 
        -! = Or <>
 
 
    -- Logical Operators
        -- and
        - so students between 18-28 information
        select * from students where age>18 and age<28;
        -- 失败select * from students where age>18 and <28;
 
 
        - 18-year-old woman
        select * from students where age>18 and gender="女";
        select * from students where age>18 and gender=2;
 
 
        -- or
        --18 checked height above or 180 (included) or more
        select * from students where age>18 or height>=180;
 
 
        -- not
        - within the above information is not 18-year-old woman this range
        -- select * from students where not age>18 and gender=2;
        select * from students where not (age>18 and gender=2);
 
        - age is not less than or equal to 18 and women
        select * from students where (not age<=18) and gender=2;
 
 
     - Fuzzy query
        -- like
        -% or more of an alternative
        - Replace one _
        - query name to "small" start name
        select name from students where name="小";
        select name from students where name like "小%";
 
        - query name in the "small" all the names
        select name from students where name like "%小%";
 
        - Query There are two words in the name
        select name from students where name like "__"; # two underscores
 
        - inquiries have names 3 words
        select name from students where name like "___";
 
        - query has at least two-word name
        select name from students where name like "__%";
 
 
        - rlike Regular
        - query name to start the week
        select name from students where name rlike "^ * Week."; # ^ represents the beginning
 
        - Queries begins with the week, ending with the name Aaron
        select name from students where name rlike ". ^ Zhou Lun $ *"; # $ represent the end
 
 
    - range queries
        - in (1, 3, 8) represents a non-contiguous within the scope
        - the name of the query aged 18, 34
        select name,age from students where age=18 or age=34;
        select name,age from students where age=18 or age=34 or age=12;
        select name,age from students where age in (12, 18, 34);
 
 
        
        - not in not a non-continuous range of
        - information between the age is not 18, 34 years old
        select name,age from students where age not in (12, 18, 34);
 
 
        - between ... and ... represents a continuous range
        Information inquiry between the ages of 18-34 -
        select name, age from students where age between 18 and 34;
 
        
        - not between ... and ... is not a continuous representation of the range
        Information Query is not the age between 18 and 34 -
        select * from students where age not between 18 and 34;    # not between 是个整体
        select * from students where not age between 18 and 34;
        -- 失败的select * from students where age not (between 18 and 34);
 
 
    - Empty judgment
        - Empty sentence is null
        - Height query information is empty
        select * from students where height is null;
        select * from students where height is NULL;
        select * from students where height is Null;
 
        - sentenced to a non-null is not null
        select * from students where height is not null;
 
 
 
- Sort
    - order by field
    - asc ascending order, i.e. ascending
    - desc descending order, namely in descending order
 
    - Query male aged between 18-34 years of age, sorted by age ascending to
    select * from students where (age between 18 and 34) and gender=1;
    select * from students where (age between 18 and 34) and gender=1 order by age;  # 默认asc
    select * from students where (age between 18 and 34) and gender=1 order by age asc;
 
 
    - 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=2 order by height desc;
    
 
    - order by multiple fields
    - Query women between the ages of 18-34 years old, height from high to low sort, under the same circumstances if the height by age from small to large
    select * from students where (age between 18 and 34) and gender=2 order by height desc,id desc;
 
 
    - Query women between the ages of 18-34 years old, height from high to low sort, under the same circumstances if the height by age from small to large,
    - If age is the same as that descending order according to id
    select * from students where (age between 18 and 34) and gender=2 order by height desc,age asc,id desc;
 
    
    - by age from small to large, tall and short of the sort from high to
    select * from students order by age asc, height desc;
 
 
- aggregate function
    - Total
    -- count
    - Query male how many people, how many people are female
    select * from students where gender=1;
    select count(*) from students where gender=1;
    select count (*) as the number of men from students where gender = 1;
    select count(*) as 女性人数 from students where gender=2;
 
 
    - maximum
    -- max
    - Query oldest
    select age from students;
    select max(age) from students;
 
    - Query women's highest height
    select max(height) from students where gender=2;
 
    - minimum
    - I
 
    
    - summation
    -- sum
    - calculation of the sum of 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(sum(age)/count(*), 2) from students;
    select round(sum(age)/count(*), 3) from students;
 
    - calculation of average height men 2 decimal places
    select round(avg(height), 2) from students where gender=1;
    -- 失败select name, round(avg(height), 2) from students where gender=1;
 
- packet (and typically used with the polymerization)
 
    -- group by
    - grouped according to sex, sex all queries
     -- select name from students group by gender;
     -- select * from students group by gender;
    select gender from students group by gender;
    -- 失败select * from students group by gender;
 
    - calculate the number of each sex in
    select gender,count(*) from students group by gender;
 
 
    - calculate the number of men
    select gender,count(*) from students where gender=1 group by gender;
 
 
    -- group_concat(...)
    - query name in the same kind of sex
    select gender, group_concat(name) from students where gender=1 group by gender;
    select gender,group_concat(name, age, id) from students where gender=1 group by gender;
    select gender,group_concat(name, "_", age, " ", id) from students where gender=1 group by gender;
 
    -- having
    - Query average age of over 30 years of age sex, and the name having avg (age)> 30
    select gender, group_concat(name),avg(age) from students group by gender having avg(age)>30;
    
    - The number of queries each sex in more than two information
    select gender, group_concat(name) from students group by gender having count(*)>2;
 
      - where and having difference:
     - where condition judgment table of elements, having the condition judgment of the results of a query
 
 
- Paging
    -- limit start, count
 
    - check out the number of data limitations
    select * from students where gender=1 limit 2;
 
    - before data query 5
    select * from students limit 0, 5;
 
    - Query id6-10 (included) book preface
    select * from students limit 5, 5;
 
 
    - 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; - -----> limit (p N -1) * each number, page number;
 
    - 2 per page, display the information on page 6, by age from small to large
    -- 失败select * from students limit 2*(6-1),2;
    -- 失败select * from students limit 10,2 order by age asc;
    select * from students order by age asc limit 10,2;
 
    select * from students where gender=2 order by height desc limit 0,2;
 
 
 
- join queries (multi-table situation)
    - Inner ON the Join ...   in the # connection
 
    -- 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.*, classes.name from students inner join classes on students.cls_id=classes.id;
    select students.name, classes.name from students inner join classes on students.cls_id=classes.id;
 
    - to the data from the table name
    select s.name, c.name from students as s inner join classes as c on s.cls_id=c.id;
 
    - Query student and class information can correspond to class, show students all the information, only the name of the class
    select s.*, c.name from students as s inner join classes as c on s.cls_id=c.id;
    
    - In the above query, the class name is displayed in the first column
    select c.name, s.* from students as s inner join classes as c on s.cls_id=c.id;
 
    - Query student and class information can correspond to class, sorted according to class
    -- select c.xxx s.xxx from student as s inner join clssses as c on .... order by ....;
    select c.name, s.* from students as s inner join classes as c on s.cls_id=c.id order by c.name;
 
    - at the time when the same class, from small to large order in accordance with student id
    select c.name, s.* from students as s inner join classes as c on s.cls_id=c.id order by c.name,s.id;
 
    -- left join  on
    - Query class information corresponding to each student
    select * from students as s left join classes as c on s.cls_id=c.id;
 
    - Student query does not correspond to class information
    -- select ... from xxx as s left join xxx as c on..... where .....
    -- select ... from xxx as s left join xxx as c on..... having .....
    select * from students as s left join classes as c on s.cls_id=c.id having c.id is null;
    select * from students as s left join classes as c on s.cls_id=c.id where c.id is null;
 
    -- right join  on
    - the name swap position data table, complete with a left join
 
- autocorrelation
    - Provincial linkage url: http://demo.lanrenzhijia.com/2014/city0605/
 
    - Query all provinces
    select * from areas where pid is null;
 
    - check out what City, Shandong Province
    select * from areas as province inner join areas as city on city.pid=province.aid having province.atitle="山东省";
    select province.atitle, city.atitle from areas as province inner join areas as city on city.pid=province.aid having province.atitle="山东省";
 
    - check out what county Qingdao
    select province.atitle, city.atitle from areas as province inner join areas as city on city.pid=province.aid having province.atitle="青岛市";
    select * from areas where pid=(select aid from areas where atitle="青岛市")
 
 
- subquery (nested in a select this other select)
    - scalar subquery
    - check out the information of above-average height
 
    - information query highest boys
    select * from students where height = 188;
    select * from students where height = (select max(height) from students);
 
    - column subquery
    - Student Information Query class students can corresponding number
    -- select * from students where cls_id in (select id from classes);
 
 

Guess you like

Origin www.cnblogs.com/WJZheng/p/11495124.html