数据库
首页 > 数据库> > Mysql02_select_1

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、逻辑运算

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 、范围查询

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、空判断

select *from stu where card=''              #找出card为’‘的数据
select *from stu where not card=''          #找出card有效的数据
select *from stu where card is NUll         #card为NULL

7、排序

select *from stu order by age asc           #按照年龄升序

8、聚合统计等

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

select *from stu group by class      
select count(*) from stu group by class 
select sex from stu group by sex

10、获取部分行

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) 不够不显示

标签:Mysql02,age,stu,limit,where,class,select
来源: https://www.cnblogs.com/zhouDEblogs/p/14408379.html