- Create Student Table
create table students ( id int unsigned not null auto_increment primary key, name varchar(20) default '', age tinyint unsigned default 0, high decimal(5,2), gender enum('男', '女', '中性', '保密') default '保密', cls_id int unsigned default 0, is_delete bit default 0 );
- to insert data table students
Students. INTO values INSERT (0, 'Bob', 18,180.00,1,1,0), (0, 'Satsuki', 19,180.00,1,2,0), (0, 'Eddie', 28,185.00,1,1 , 0), (0, 'Andy', 58,175.00,1,2,0), (0, 'Huang Rong', 108,160.00,2,1,0), (0, 'Feng', 44,150.00,4,2,1 ), (0, 'WangZuXian', 52,170.00,2,1,1), (0, 'Jay children', 34 is, null, 1,1,0), (0, '- Kun', 44,181.00,1,2 , 0), (0, 'and Shen', 55,166.00,1,2,0), (0, 'Crystal Liu', 29,162.00,2,3,0), (0, 'Venus', 45,180.00,3,4, 0), (0, 'Shizuka', 18,170.00,2,4,0), (0, 'Guo Jing', 22,167.00,1,5,0), (0, 'Zhou Jie', 33,178.00,1,1,0 ), (0, 'Chin Siu-ho', 56,178.00,1,1,0), (0, 'Nicholas', 38,175.00,1,1,0), (0, 'Edison', 38,175.00,1,1, 0);
Simple query:
- query all the columns
--select * from table name
select * from students;
- certain conditions query (where)
select * from where id=5;
- formulate queries column
select id_name from students;
- from the field to use as an alias
select id_name as 'alias (Alias)' SELECT name AS 'name', age as 'aged', high, gender from students;
- Query by table name field
select the library name / Field / * from students;
select * from students; select students.* from students; eg: mysql > select test.students.* from test.students;
- alias the query to the table
select s.id,s.name,s.age from students as s;
- Eliminate duplicate rows
- distinct
select distinct age from students;
Conditions inquiry
- comparison operator
- Query information older than 18 years
select * from students where age > 18;
Between --18-year-old to 28 years old (and)
select * from students where age >= 18 and age =< 28; select * from students where age >= 18 || age =< 28;(有时会报错) select * from students where age between 18 and 28
- People over 18 years of age or a height of more than 180 (or)
select * from students where age > 18 or high > 180;
- Query the age equal to 18 years of age and older people equal to 28
select * from students where age=18 or age=28;
Fuzzy query:
- like
% or alternatively a plurality of or no
- query name has 'small' all names
select * from students where name like '%小%';
- Query the word person's name
select * from students where name like '_ _'; (_ represents a character)
- query has at least two-word name
select * from students where name like '%_ _%';
Range queries
- in (1,3,8) represents a non-contiguous within the range
- queries for the young people of 18 and 34
select * from students where age in (18, 34); select * from students where age=18 or age=34;
- Query young people 18 and 34 do not include the (inverted)
select * from students where age not in (18, 34);
- Query information between the ages of 17 and 34 years
select * from students where age between 17 and 34;
- Query not older 18-34-year-old information
select * from students where age not between 17 and 34;
Empty judgment
- judgment is null
- Height query information is empty
select * from students where high is null;
- to determine non-empty is not null
select * from students where high is not null;
Sequence
- order by fields (EG: name, Age, the above mentioned id)
- asc ascending order, that is, in ascending order (the default is not written asc)
- desc descending order, namely in descending order
- the old inquiry 18-34 males between the age, according to age, from small to large
select * from students where gender=1 and age between 18 and 34 order by age;
- Query older women between 18-34 years of age, height from high to low
select * from students where gender=2 and age between 18 and 34 order by high desc;
- order by multi-field
- query in older women aged 18-34, height from high to low sort, from small to large sorted by age, if under the same circumstances height
select * from students where age between 18 and 34 and gender=2 order by high desc;
-- 按照年纪、身高排序(如果年纪相同的情况下按照身高从小到大排序)
select * from students where order by age,high asc;
-- 查询年纪在18到34岁的男性,身高从高到矮排序,如果身高相同的情况下按照年纪从小到大排序,
如果年龄也相等那么按照id从小到大排序;
select * from students where age between 18 and 34 and gender=1 order by high desc, age, id desc;
聚合函数
-- 总数
-- count
-- 查询有多少人
select count(name/age/high) from students;
(统计当前字段,如果要统计的数据为null(空),会出现统计错误,所以一般用*)
-- 查询有多少人(并且显示中文)
select count(*) as '总人数' from students;
-- 查询男性有多少人
select count(*) from students where gender=1;
-- 最大值max
-- 查询最大的年纪
select max(age) from students;
-- 查询女性的最高 身高
select max(high) from students where gender=2;
-- 最小值min
-- 查询最高的人
select min(high) from students;
-- 求和sum
-- 计算所有人的年龄总和
select sum(age) from students;
-- 平均值avg
-- 计算平均年纪
-- 计算平均年纪 sum(age)/count(*)
select sum(age)/count(*) from students; select avg(age) from students;
-- 保留2位小数
select round(avg(age),2) from students;
分组(分组的优先级最高,一条语句中先分组)
-- group by
-- 按照性别分组,查询所有的性别
select gender from students group by gender;
-- 计算每组性别的人数
select gender,count(*) from students group by gender;
--group_concat(函数,和group by配合使用)
-- 查询性别组中的男女平均年龄及姓名
select gender,group_concat(name),avg(age) from students group by gender;
-- having
-- 查询每个性别平均年纪超过30岁的性别,以及姓名 having avg(age) > 30
select gender, group_concat(name) from students group by gender having avg(age) > 30;
-- 查询每种性别中的人数多于4个的组的信息
select gender,group_concat(name) from students group by gender having count(*)>4;
分页
-- 显示5页
select * from students limit 5;
-- 查询年纪最大的人名(先按照年纪排序,再分页)
select * from students order by age desc limit 1;
-- 分页显示,每页显示2条数据
select * from students limit 0,2;
-- 按照身高从高到矮排序,查找出所有女性,并且分页显示,每页显示2条数据
select * from students where gender=2 order by high desc limit 0,2;