Mysql02_select_1

1、简单查询

select name from stu           #列出所有的name
select name as NAME from stu   #修改显示的表头

2、条件查询

  • = < > !=
select *from stu where name='Zhang'
select *from stu where class='02'
select *from stu where age<20
select *from stu where HomeTown!='SHANXI' 

3、逻辑运算

  • and or not
select *from stu where age<20 and sex='NAN'      #年龄小于20的男生
select *from stu where class='01' or class='02'  #1班或者2班的学生
select *from stu not HomeTown='SHANXI'           #不是’SHXANXI‘的数据,同!=

4 、模糊查询

% 匹配任意多个字符

_ 匹配一个任意字符

select *from stu where name like 'Zhang%'        #匹配Zhang***
select *from stu where name like 'Zhang_'        #匹配Zhang*

5 、范围查询

  • in('val1','val2','val3') 用or可实现
  • between--and-- 用判断可实现
select *from stu where class='1' or class='2' class='3'    #用or实现
select *from stu where class in('1','2','3')               #in
select *from stu where age>=18 and age<=22           #用判断
select *from stu where age between 18 and 22         #用between

6、空判断

  • NULL和’ ‘是不相同的
  • 判断空使用is NULL
select *from stu where card=''              #找出card为’‘的数据
select *from stu where not card=''          #找出card有效的数据
select *from stu where card is NUll         #card为NULL

7、排序

  • order by -- asc 升序
  • order by -- desc 降序
select *from stu order by age asc           #按照年龄升序

8、聚合统计等

  • count 统计
  • max min 最大最小值
  • sum 求和
  • avg 平均值
select count(*) from stu                  #数量
select max(age) from stu where class='02' #'02'班最大年龄
select avg(age) from stu where class='02' #'02'班平均年龄

9、分组

group by

  • 查看有几种数据(执行后显示所有的class 每种只显示一条数据)
  • 结合count使用即可得到类型总数
select *from stu group by class      
select count(*) from stu group by class 
  • 查看各种类型
select sex from stu group by sex

10、获取部分行

  • limit x,y (截取起始位置x+1,数量y)
select *from stu limit 1,3   #从第2条开始取3条数据

分页实现

  • 先排序 取前三行(默认升序)
select *from stu ordey by age limit 0,3
  • 分页
select count(*) from stu             #获取行数
select *from stu limit 0,5           #从第1条取5条为P1  (1-2-3-4-5)
select *from stu limit 5,5			 #从第6条取5条为P2  (6-7-8-9-10)
select *from stu limit 10,5			 ##从第11条取5条为P3(11-12-13) 不够不显示
posted @ 2021-02-17 10:49  Zhou_DE_blogs  阅读(46)  评论(0编辑  收藏  举报