单表数据查询

一.避免重复查询-distinct

    语法格式:select distinct 字段一,字段二 from student

 

select distinct groupNum,qq from student

 

二.实现数学运算的查询

MySQL中的数学运算符:+ - * / %

select name,salary*12 as 年收入 from student 

三.concat连字符查询

     格式化显示查询结果:使用字符串拼接函数将查询出来的数据进行再加工

    as是给查询出来的字段起的别名,在后续做视图查询或者应用程序对字段进行解析的时候会起到作用

1 -- concat连字符查询
2 select CONCAT('皇家大应用-',name,'同学') as 学生,email from student

四.条件记录的数据查询

  4.1 带关系运算符和逻辑运算符的表达式

        MySQL中的关系运算符:> 、>=、<、<=、!=(<>)、=

        MySQL中的逻辑运算符:&&(and)‖(or)、!(not)

1 -- 下面例子使用到了算术运算符和逻辑运算符
2 -- 在逻辑表达式中,我们一般用and和or来表示
3 select name,salary from student where salary>=3500 and salary<=4000

4.2 带between and关键字的条件查询

 

1 -- between and关键字查询
2 -- 其中查询出来的结果集包含了4000和4500,在实际开发中,对日期类型的数据查询场景使用较多
3 select name,salary from student where salary between 4000 and 4500

 

-- 查询月薪不在4000到4500的
select
name,salary from student where not(salary between 4000 and 4500)

 

4.3  is null关键字查询

 

1 -- is null关键字查询
2 -- 先设置一下
3 -- update student set code=null,password=null,salary='',email='' WHERE name="林英方"
4 select name from student where  code is null and  password is null 
5 select name from student where salary='' or email=''

 

 

 

 

其中不为空表示为is not null

select name from student where salary is not null

4.4 带in关键字的数据条件查询

 温馨小提示:查询的是一个集合

select id,name from student where id in(1,3,5)

 

select id,name from student where id not in(1,3,5)

温馨小提示:不在某个集合范围内的查询

 

select id,name from student where id not in(1,3,5)

 

 

4.5 带like关键字的查询

 

1 -- like关键字模糊查询
2 select name from student where name like '阿%'-- 以阿开头,后面0~任意个字
3 select name from student where name like '%果子'-- 前面0~任意个字,以果子结尾

 


 

 五.排序查询

1 -- 排序查询 asc升序(从小到大) desc降序(从大到小)
2 select * from student as s order by s.groupNum asc,salary desc

温馨小提示:前面是将groupNum字段升序(从小到大),在groupNum相同的时候以salary字段以降序(从大到小)方式排序

六.限制数据记录的查询

 

1 -- 限制数据记录的查询数量
2 -- 在实际的程序开发中,我们不可能一次性的把所有数据都查询出来
3 -- 然后在服务器中对数据进行分页的过滤
4 -- 所以我们在数据查询的时候自己进行分页
5 -- limit后面的两个参数代表着从哪一行开始,查询几行记录
6 select * from student as s order by s.groupNum asc,s.salary desc LIMIT 0,3

 

七.统计函数和分组数据查询

7.1 统计查询

除了count函数外,其它函数如果没有找到记录,返回值是null,count函数返回0

1 -- 统计函数查询
2 select avg(salary) as 平均薪水 from student -- 平均数
3 select max(salary) as 最高薪水 from student -- 最大值
4 select min(salary) as 最低薪水 from student -- 最低值
5 select sum(salary) as 所有人薪水总和 from student-- 求和
6 select count(*) from student-- 代表student表中有多少条记录
7 select avg(salary) as 平均薪水 from student  where id='-1'-- 平均数
8 select count(*) from student where groupNum='10' -- groupNum等于10的记录有几条

       

 

    

 

          

7.2  分组查询

 

 1 -- 分组查询执行顺序分析
 2 -- 先以groupNum字段执行分组,查询groupNum字段
 3 -- group_concat(name)每组中的成员姓名
 4 -- 再计算出每组的平均薪水
 5 select groupNum ,GROUP_CONCAT(name),AVG(salary)
 6 from student 
 7 group by groupNum
 8 
 9 -- 先以groupNum字段执行分组,再以age字段执行分组
10 -- 查询groupNum,age
11 -- 查询出同一组且同一年龄有哪些成员
12 select groupNum,age,GROUP_CONCAT(name) 
13 from student 
14 group by groupNum,age

 

 

 


7.3 使用HAVING对聚合完的数据进行条件限制查询

 

 

 1 -- 使用having对聚合完的数据进行条件限制查询
 2 -- having就是用来聚合函数进行条件过滤的
 3 -- 先执行age='20'进行数据过滤
 4 -- 在进行group by分组
 5 -- 再执行select后的所有代码
 6 -- 最后having对聚合完成的数据进行再次过滤
 7 select groupNum,GROUP_CONCAT(name),avg(salary)
 8 from student 
 9 where age='20'
10 group by groupNum
11 HAVING avg(salary)>3500

 

 

posted @ 2018-12-12 09:24  静心*尽力  阅读(325)  评论(0编辑  收藏  举报