mraiadb check

- basic query used (conditions, sort, aggregate functions, packet paging)


- Create a table of students
the Create the Table Students (
the above mentioned id unsigned int not null Primary Key AUTO_INCREMENT,
name VARCHAR (20) default '',
Age tinyint unsigned default 0,
High decimal (5,2),
Gender enum ( 'male', 'female ',' neutral ',' Confidential ') default' Confidential ',
cls_id unsigned int default 0,
is_delete 0' bit default
);


--创建班级表
create table classes(
id int unsigned auto_increment primary key not null,
name varchar(20) not null
);

- to insert data table students
INSERT INTO students values
(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, 'Cheng gon ', 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);
Source
- query
- query all the columns
--select * from table name
* Students from the SELECT;

- certain conditions query (the WHERE)
the SELECT * from table name the above mentioned id = 5 the WHERE;
the SELECT * Students from the WHERE the above mentioned id = 15;

- the query formulation columns
the SELECT the above mentioned id, name from Students;


- to use as field since the alias
select id, name as 'name', Age, High, Gender from Students;
the SELECT name AS 'name', age as 'old' from Students;

- query table name field by
the SELECT students.name from Students;

- alias to the table from the query
SELECT s.id, s.name, Students. s.age from AS S;

- eliminate duplicate rows
- DISTINCT
SELECT DISTINCT from Students. Age;



--条件查询
--比较运算符
-- 查询年纪大于18岁的信息
select * from students where age > 18;

--18岁到28岁之间(and)
select * from students where age >= 18 and age =< 28;
select * from students where age > 18 && age <=30;

select * from students where age between 18 and 28

-在18岁以上或者身高180以上的人(or)
select * from students where age > 18 or high > 180;
select * from students where age >20 || high >180;

-- 模糊查询
-- like
-- % 替代1个或者多个甚至是没有
-- 查询姓名中有‘小’的所有名字
select * from students where name like '%小%';

-- 查询两个字人的名字
下划线代表代表一个的意思
select * from students where name like '__';

-- 查询至少有2个字的名字
select * from students where name like '%__%';



--范围查询
-- in (1,3,8)表示在一个非连续的范围内
-- 查询 年纪为18和34的人
select * from students where age in (18, 34);


--查询 年龄在17岁到34岁之间的信息
select * from students where age between 17 and 34;


--查询 年纪不在18到34岁的信息
select * from students where age not between 17 and 34;

-- 空判断
-- 判断is null
-- 查询身高为空的信息
select * from students where high is null;

-- 判断非空is not null
select * from students where high is not null;

-- 排序
-- order by 字段
-- asc从小到大排列,即升序
-- desc从大到小排序,即降序

-- 查询年纪在18到34岁之间的男性,按照年纪从小到大
select * from students where gender=1 and age between 18 and 34 order by age;


-- 查询年纪在18到34岁之间的女性,身高从高到矮
select * from students where gender=2 and age between 18 and 34 order by high desc;


-- 查询年纪在18到34岁的女性,身高从高到矮排序,如果身高相同的情况下按照年纪从小到大排序
select * from students where age between 18 and 34 and gender=2 order by high desc;

-- 查询年纪在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(*) 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),2 from students;
-- 保留2位小数
select round(avg(age),2) from students;
整数
-- 分组
-- group by
-- 按照性别分组,查询所有的性别
select gender from students group by gender;

+--------+
| gender |
+--------+
| 男 |
| 女 |
| 保密 |
| 中性 |
+--------+

-- 计算每组性别的人数
select gender, count(*) from students group by gender;
| gender | count(*) |
+--------+----------+
| 男 | 20 |
| 女 | 4 |
| 保密 | 1 |
| 中性 | 1 |


-- 查询男性组中的姓名 group_concat
select gender,group_concat(name,',',age ,'|') from students where gender=2 group by gender;


| 女 | 静香,18 | ,刘亦菲,29 | ,王祖贤,52 | ,黄蓉,108 |
-- having
-- 查询每个性别平均年纪超过30岁的性别,以及姓名 having avg(age) > 30
select gender, group_concat(name) from students group by gender having avg(age) > 30;
select gender, avg(age) from students group by gender having avg(age)>10;

+--------+----------+
| gender | avg(age) |
+--------+----------+
| 男 | 31.7000 |
| 女 | 51.7500 |
| 保密 | 45.0000 |
| 中性 | 44.0000 |
-- 查询每种性别中的人数多于4个的组的信息
select gender,group_concat(name) from students group by gender having count(*)>4;
-- 分页
-- 显示5页limit
select * from students limit 5;

-- 分页显示,每页显示2条数据
select * from students limit 0, 2;

-- 按照身高从高到矮排序,查找出所有女性,并且分页显示,每页显示2条数据
select * from students where gender=2 order by high desc limit 0,2;

Guess you like

Origin www.cnblogs.com/itzhao/p/11287212.html