MySQL基础(查)初级

创建数据表:

drop table if exists students;
create table students (
  studentNo varchar(10) primary key,
  name varchar(10),
  sex varchar(1),
  hometown varchar(20),
  age tinyint(4),
  class varchar(10),
  card varchar(20)
)

准备数据

insert into students values
('001', '王昭君', '女', '北京', '20', '1班', '340322199001247654'),
('002', '诸葛亮', '男', '上海', '18', '2班', '340322199002242354'),
('003', '张飞', '男', '南京', '24', '3班', '340322199003247654'),
('004', '白起', '男', '安徽', '22', '4班', '340322199005247654'),
('005', '大乔', '女', '天津', '19', '3班', '340322199004247654'),
('006', '孙尚香', '女', '河北', '18', '1班', '340322199006247654'),
('007', '百里玄策', '男', '山西', '20', '2班', '340322199007247654'),
('008', '小乔', '女', '河南', '15', '3班', null),
('009', '百里守约', '男', '湖南', '21', '1班', ''),
('010', '妲己', '女', '广东', '26', '2班', '340322199607247654'),
('011', '李白', '男', '北京', '30', '4班', '340322199005267754'),
('012', '孙膑', '男', '新疆', '26', '3班', '340322199000297655')

查询所有字段:
语法:# select * from 表名
例:select * from students
查询指定字段:
语法:select 列1,列2,...from 表名
在select后面的列名部分,可以使用as为列起别名,这个别名出现在结果集中。

--表名.字段名
select students.name,students.age from students
--可以通过as给表起别名
select s.name as "姓名",s.age as "年龄" from students as s
-- 如果是单表查询 可以省略表名
select name,age from students

消除重复行:
在select后面列前使用distinct可以消除重复的行
语法:select distinct 列1,.... from 表名;
例:select distinct sex from students

条件

使用where子句对表中的数据筛选,符合条件的数据会出现在结果集中:
语法:select 字段1,字段2... from 表名 where 条件;
例:select * from students where id = 1
where后面支持多种运算符,进行条件的处理

  1. 比较运算
  2. 逻辑运算
  3. 模糊查询
  4. 范围查询
  5. 空判断

比较运算符
等于(=)、大于(>)、大于等于(>=)、小于(<)、小于等于(<=)、不等于(<>/!=)
例子:
例1:

--查询小乔的年龄
select age from students where name = "小乔"

例2:

--查询20岁以下的学生
select * from students where age<20

例3:

-- 查询家乡不在北京的学生
select * from students where hometown!="北京"

练习:
1、查询学号是’007’的学生的身份证号

select * from students where studentNo = "007"

2、查询"1班"以外的学生信息

select * from students where class!="1班"

3、查询年龄大于20的学生的姓名和性别

select name as "姓名",sex as "性别", age as "年龄"from students where age>20

逻辑运算符
and
or
not
例子:
例1:

--查询年龄小于20的女同学
select * from students where age<20 and sex="女"

例2:

--查询女同学或"1班"的学生
select * from students where sex="女" or class="1班"

例3:

--查询非天津的学生
select * from students where not hometown="天津"

练习:
1、查询河南或者河北的学生

select * from students where hometown="河南" or hometown="河北"

2、查询"1班"的"上海"的学生

select * from students where class="1班" and hometown="上海"

3、查询非20岁的学生

select * from students where not age="20"

模糊查询
like
%表示任意多个任意字符
_表示一个任意字符
例子:
例1

--查询姓孙的学生
select * from students where name like "孙%"

例2

--查询姓孙且名字是一个字的学生
select * from students where name like "孙_"

例3

--查询叫乔的学生
select * from  students where name like"%乔"

例4

--查询姓名含白的学生
select * from students where name like ""

练习:
1、查询姓名为两个字的学生

select * from students where name like"__"

2、查询姓百且年龄大于20的学生

select * from students where name like"百%" and age>20

3、查询学号以1结尾的学生

select * from students where studentNo like"%1"

范围查询
in表示在一个非连续的范围内
例子

--查询家乡是北京或上海或广东的学生
select * from students where hometown in("北京","上海","广东")

between… and…表示在一个连续的范围内
例子

--查询年龄为18至20的学生
select * from students where age between 18 and 20

练习:
1、查询年龄在18或19或22的女生

select * from students where sex="女" and age in(18,19,22)

2、查询年龄在20到25以为的学生

select * from students where not age between 20 and 25

空判断
注意: null与’'是不同的
判空
is null

--查询没有填写身份证的学生
select * from students where caerd is null

判非空
is not null

--查询填写了身份证的学生
select * from students where card is not null

排序

为了方便查看数据,可以对数据进行排序
语法:select * from 表名 order by 列1 asc | desc, 列2 asc|desc,...
将行数据按照列1进行排序,如果某些行列1的值相同时,则按照列2排序,以此类推。
默认按照列值从小到大排列。
acs从小到大排列,即升序
desc从大到小排序,即降序
例子
例1

--查询所有学生的信息,按年龄从小到大排序
select * from students order by age

例2

--查询所有学生信息,按年龄从大到小排序,年龄相同时,再按学号从小到大排序
select * from students order by age desc,studentNo

练习:
查询所有学生信息,按班级从小到大排序,班级相同时,再按学号从小到大排序

select * from students order by class,studentNo

聚合函数

为了快速得到统计数据,经常会用到如下5个聚合函数

  1. count()表示计算总行数,括号中写与列名,结果是相同的(聚合函数不能在where中使用)
  2. max(列)表示求此列的最大值
  3. min(列)表示求此列的最小值
  4. sum(列)表示求此列的和
  5. avg(列)表示求此列的平均值

例子:
列1

--查询学生总数
select count(*) from students

例2

--查询女生的最大年龄
select max(age) from students where sex="女"

例3

--查询1班的最小年龄
select min(age) from students where class="1班"

例4

--查询北京学生的年龄总和
select sum(age) from students where hometown="北京"

例5

--查询女生的平均年龄
select avg(age) from students where sex="女"

练习
1、查询所有学生的最大年龄、最小年龄、平均年龄

select max(age) as "最大年龄",min(age) as "最小年龄",avg(age) as "平均年龄" from students

2、一班共有多少个学生

select count(*) from students where class="1班"

3、查询3班年龄小于18岁的同学有几个

select count(*) from students where class="3班" and age<"18"

分组

按照字段分组,表示此字段相同的数据会被放在一个组中。
分组后,分组的依据列会显示在结果集中,其他列会不显示在结果集中。
可以对分组后的数据进行统计,做聚合运算。
语法:select 列1,列2,聚合... from 表名 group by 列1,列2...
例子:
例1

--查询各种性别的人数
select sex,count(*) from students group by sex

例2

--查询各种年龄的人数
select age,count(*) from students group by age

练习:
查询各个班级学生的平均年龄、最大年龄、最小年龄

select class as "班级",avg(age) as "平均年龄",min(age) as "最小年龄", max(age)  as "最大年龄"  from students GROUP BY class

分组后的数据筛选
语法: select 列1,列2,聚合... from 表名 group by 列1,列2,列3... having 列1,...聚合...
例子:
例1
查询男生总人数
方案一:

select count(*) from students where sex="男"

方案二:

select sex,count(*) from students group by sex having sex="男"

练习
查询1班除外其他班级学生的平均年龄、最大年龄、最小年龄
对比where与having
where是对from后面指定的表进行数据筛选,属于对原始数据的筛选;
having是对group by的结果进行筛选;

分页

获取部分行
当数据量过大时,在第一页中查看数据是一件非常麻烦的事情
语法:

select * from 表名 limit start,count
从start开始,获取count条数据
start索引从0开始

例子

--查询前三行学生信息
select * from students limit 0,3

练习
查询第4到第6行学生信息

select * from students limit 3,3

分页
已知:每页显示m条数据,求:显示第n页的数据

select * from students limit (n-1)*m,m

求总页数
查询总页数p1
使用p1除以m得到p2
如果整除则p2为总数页
如果不整除则p2+1为总页数
练习
每页显示5条数据,显示每一页的数据

select * from students limit 0,5

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值