mysql select 1=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、逻辑运算

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值