MySQL--DQL--单表查询

本文详细介绍了SQL的基本操作,包括创建表、插入数据、简单查询、条件查询、排序查询、处理函数及聚合函数等,帮助读者快速掌握SQL核心技能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先在任何一个数据库下创建一个表:

create table student(
					id int ,
					name varchar(20),
					gender varchar(10),
					sorce double(4,2)
);

添加一些数据:

insert into student values(1,"zhangsan","男",82.23),
						  (2,"lisi","男",97.29),
			 	 	  	  (3,"wangwu","女",85.28);

1.简单查询

  • 语法:select 字段1,字段2… from 表名;
  • 查询学生姓名:
    select name from student;
  • 查询学生全部信息:
    select * from student;
  • 查询出来的字段名称进行重命名
    select name ,gender xingbie from student;

2.条件查询

  • 语法: select 字段1,字段2… from 表名 where 条件;
  • 条件查询需要使用where,并且where需要放到from 表的后面
  • 查询id是2的学生:
    select * from student where id=2;
  • 查询成绩不等于80分的学生:
    select name from student where score<>80;
  • 查询成绩在80-90分的学生:
    select name from student where score>=80 and score<=90;
使用between…and…
  • 查询成绩在80-90分的学生:
    select name from student where score between 80 and 90;
and(多个条件结合起来查询)
  • 查询性别为男,并且成绩大于90分
    select * from student where gender=“男” and score>90;
or(多个条件满足一个即可)
  • 查询性别为男,或者成绩大于90分
    select * from student where gender=“男” or score>90;
  • 注意:where后面的条件同时有and和or,and的优先级大于or
in (多个条件满足一个即可,比or简洁)
  • 查询id为1或者2的学生:
    select name from student where id in (1,2);
  • 查询id不为1和2的学生:
    select id,name from student where id not in (1,2);
like,模糊查询,where后面使用 like
  • 查询姓名以z开头的所有学生:
    select * from student where name like “z%”;
  • 查询姓名以i结尾的所有学生:
    select * from student where name like “%i”;
  • 查询姓名中包含h的所有学生:
    select * from student where name like “%h%”;
  • 查询姓名中第二个字母是h的所有学生:
    select * from student where name like “_h%”;

3.排序查询 order by

(asc:从小到大,desc:从大到小,默认是asc,默认可以不写)
  • 语法:select 字段1,字段2… from 表名 order by 字段a 排序方式 ,字段b 排序方式;
  • 按照成绩从小到大排序;
    select name ,score from student order by score ;
  • 按照成绩从大到小排序;
    select name ,score from student order by score desc;
  • 多个字段排序,按照score和name倒序排序:
    select * from student order by score desc,name desc;

4.处理函数

lower()转换为小写
  • 将学生姓名全部转换为小写:
    select lower(name) l_name from student;
upper()转换为大写
  • 将学生姓名全部转换为大写:
    select upper(name) u_name from student;
substr()截取子串
  • 查询所有学生姓名的第二个字母:
    select substr(name,2,1) sub_name from student;
  • 查询学生姓名中第二个字母为h的所有学生:
    第一种方法:select name from student where substr(name,2,1)=‘h’;
    第二种方法:select name from student where name like ‘_h%’;
length()获取字段长度
  • 获取所有学生姓名的长度:
    select length(name) length_name from student;
ifnull(字段名,替换值),空值处理

查询学生的成绩,如果为null设置为0;
select name,ifnull(score,0) score2 from student;

trim()去除首尾空格
  • 获取名为zhangsan同学的所有信息:
    select * from student where name=trim(’ zhangsan ');
round(数字,保留的小数位数)四舍五入
  • 查看所有学生保留一位的成绩:
    select round(score,1) from student ;
rand()生成一个0~1之间的的随机数,包含0和1
  • 生成0~100之间的随机数:
    select round(rand()*100), name from student;
str_to_date(‘日期字符串’,‘日期格式’),字符串转日期
date_format(日期类型数据,‘日期格式’),格式化日期

5,聚合函数

  • 注意:聚合函数在计算时会自动忽略空值,不用手动写sql将空值排除。
    聚合函数不能直接写在where语句的后面。
  • 创建一个新的学生表:
create table student2(
					id int , 
					name varchar(20),
					fenzu varchar(5),
					math double(5,2),
					english   double(5,2)
					);

添加一些数据:

insert into student2 values(1,"rose","A",78.82,89.34),
						   (2,"jack","A",87.23,98.67),
					   	   (3,"tom","B",78.45,92.24),
						   (4,"wen","B",98.20,76.25);
sum()求和函数
  • 查看数学成绩总和:
    select sum(math) s_math from student2;
  • 查看数学的平均成绩:
    select avg(math) a_math from student2;
max()取得最大值函数
  • 查看数学最高分:
    select max(math) m_math from student2;
min()取得最小值函数
  • 查看数学最低分:
    select min(math) m_math from student2;
count()取得数据总数
  • 查看全部人数:
    select count(id) from student2;
  • 查看数学成绩不为null的人数:
    select count(math) from student2;
  • 查看数学成绩为null的人数:
    select count(*) from student2 where math is null;
distinct将查询结果中某一字段的重复记录去除掉
  • 语法:select distinct 字段名1,字段名2… from 表名;
  • 查看有哪些组:
    select distinct fenzu from student2;
  • 统计有几个组:
    select count(distinct fenzu) from student2;

6.分组查询

group by分组
  • 统计每个组中数学成绩最高分:
    select max(math) m_math from student2 group by fenzu;
having过滤,如果想对分组的数据进行过滤,需要使用having子句。
  • 查询每组的英语平均成绩,显示出平均成绩大于80分的组
    select fenzu, avg(english) from student2 group by fenzu having avg(english)>80;
  • 注意:where和having区别
    where和having都是为了完成数据的过滤,它们后面都是添加条件;
    where是在 group by之前完成过滤;
    having是在group by之后完成过滤;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值