SQL 语句(单表查询)

00.数据准备

0.1 创建表

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)
);

0.2 插入数据

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');

01.查询基本语法

1.1 查询所有字段

select * from students

1.2 查询部分字段

select name,sex,age from students

1.3 取别名

select name as 姓名,sex as 性别,age as 年龄 from students;

1.4 去重 distinct

# 例:查询所有学生的性别,不显示重复的数据
select distinct sex from students;

02.条件查询 where

2.1 比较运算符

  • 等于: =

  • 大于: >

  • 大于等于: >=

  • 小于: <

  • 小于等于: <=

  • 不等于: != 或 <>

select age from students where name='小乔'      # 查询小乔的年龄
select * from students where age<20          # 查询20岁以下的学
select * from students where hometown != '北京'    # 查询家乡不在北京的学生

2.2 逻辑运算符

  • 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='天津'

2.3 模糊查询 like

  • like

  • %表示任意多个任意字符

  • _表示一个任意字符

# 例1:查询姓孙的学生select * from students where name like '孙%'-

2.4 范围查询

  • in表示在一个非连续的范围内
  • between ... and ...表示在一个连续的范围内
# 例1:查询家乡是北京或上海或广东的学生select * from students where hometown in('北京','上海','广东')
# 例2:查询年龄为18至20的学生select * from students where age between 18 and 20

2.5 空判断

  • 注意:null与' '是不同的

  • 判空is null

# 例1:查询没有填写身份证的学生select * from students where card is null
# 例2:查询填写了身份证的学生select * from students where card is not null

03.排序 order by

3.1 语法

select * from 表名order by 列1 asc|desc,列2 asc|desc,...
  • 将行数据按照列1进行排序,如果某些行列1的值相同时,则按照列2排序,以此类推

  • 默认按照列值从小到大排列

  • asc从小到大排列,即升序

  • desc从大到小排序,即降序

3.2 示例

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

04.聚合函数

4.1 语法说明

  • 使用聚合函数方便进行数据统计

  • 聚合函数不能在 where 中使用

  • 常用聚合函数

    • count(): 查询总记录数

    • max(): 查询最大值

    • min(): 查询最小值

    • sum(): 求和

    • avg(): 求平均值

4.2 基本使用

# 1) 示例:查询学生总数select count(*) from students;
# 2) 查询最大值select max(age) from students where sex='女';
# 3) 查询最小值select min(age) from students;
# 4) 求和select sum(age) from students where hometown='北京';
# 5) 平均值select avg(age) from students where sex='女'

05.分组 group by

5.1 分组基本使用

# 例1:查询各种性别的人数
select sex,count(*) from students group by sex
# 例2:查询每个班级中各种性别的人数
select class,sex,count(*) from students g up by class,sex

5.2 分组后的数据筛选 后接 having

  • having后面的条件运算符与where的相同

  • 例1:查询男生总人数

# 方案一
select count(*) from students where sex='男'
# 方案二
select sex,count(*) from students group by sex having sex='男'

5.3 对比 where 与 having

  • where 是对 from 后面指定的表进行数据筛选,属于对原始数据的筛选

  • having 是对 group by 的结果进行筛选

  • having 后面的条件中可以用聚合函数,where 后面不可以

06.分页

6.1 基本使用

  • 从start开始,获取count条数据

  • start索引从0开始

# 例1:查询前3行学生信息
select * from students limit 0,3

6.2 分页查询

  • limit典型的应用场景就是实现分页查询

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

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

例如:每页10条数据,显示第100页
select * from student limit (100-1)*10,10
及 (100-1)*10 = 990条,数据会从991 ~ 1000条显示出来

原文地址:https://www.cnblogs.com/chao460/p/14752774.html