当前位置: 首页 > news >正文

数据库中单表的查询(select)

单表查询

所有的查找都会得到一张虚拟表

一、 最简单的查询
SELECT 123;
SELECT 'asd';
SELECT 1+1;
二、 从表中获取数据
select 字段名,字段名 from 表名 
2.1 全字段查询
SELECT sid,sname,birthday,ssex,classid FROM student;
SELECT * FROM student;     -- 使用*不利于sql优化,不推荐
2.2 部分字段查询
SELECT sname,ssex FROM student;
2.3 字段名起别名
select sname as '姓名',birthday '生日', ssex 性别 from student;
2.4 添加字段
select sname,'yjy' 学校 from student
2.5 distinct
-- 所有的字段的数据要一致才会去重select distinct sid,sname,ssex from student
2.6 带条件的查询
(1)where 子句过滤查询结果,以仅返回满足特定条件的记录
select * from student where sid=5;
select * from student where sid<>5;
select * from student where sid>5;
select * from student where sid between 3 and 6;
示例:
-- 查找1班的女同学select * from student where classid=1 and ssex='女';-- 查询 年龄 大于1990-1-1 的学生select * from student where birthday < '1990-1-1';
(2)in 在某个特定的范围
-- 3  5  7  9
-- or 会让索引失效select * from student where sid = 3 or sid = 5 or sid = 7 or sid = 9;
-- 推荐 
-- in 可以用到索引select * from student where sid in(3,5,7,9)
(3)like 模糊查询模糊符号:a. % 任意多的任意字符b. _ 一个任意字符
insert into student(sname) values('杨佳佳'),('小杨'),('杨同学');insert into student(sname) values('帅气的杨同学');select * from student where sname like '%杨%'select * from student where sname like '杨%'select * from student where sname like '%杨'select * from student where sname like '杨_'select * from student where sname like '杨__'   -- 两个下划线
(4)null
select * from student where birthday is nullselect * from student where birthday is not null
2.7 聚合函数
把多个值变为一个值a.count() 统计个数b.max() 求最大值c.min() 求最小值d.sum() 求总和e.avg() 求平均数
-- count 不统计null
-- select count(字段\常量\*) from 表名select count(sid) from student;	-- 主键select count(classid) from student;	-- 不统计nullselect count('a') from student;		-- 不推荐select count(1) from student;	-- 推荐select count(*) from student;	-- 推荐
-- sum avg min max  数值类型select sum(score) from sc;select avg(score) from sc;select max(score) from sc;select min(score) from sc;
示例:
-- 统计出成绩表中一共有多少次考试,总成绩,平均分,最高分,最低分select count(*),sum(score),avg(score),max(score),min(score) from sc;
2.8 分组 group by
对所有的数据进行分组统计,分组的依据字段可以有多个,并依次分组
-- 统计各班有多少人select classid,count(1) from student group by classid -- 成绩表 统计每个同学的 总分和平均分select sid,sum(score),avg(score) from sc group by sid
2.9 having
与group by结合使用,进行分组后的数据筛选
-- 查询平均分不及格的sidselect sid ,sum(score),avg(score) from sc group by sid having avg(score) < 60
2.10 order by 排序
(1)按照先写的字段排序,如果有相同的按照下一个字段排序
(2)升序 asc(默认)
(3)降序 desc 必须声明
示例:
select * from student order by classid desc;	-- desc:降序
select * from student order by classid asc; -- asc:升序
select * from sc order by score desc,cid desc;
2.11 limit 分页

0 开始 (页码-1)*步长,步长

-- select * from student limit 位置,步长select * from student limit  6,3-- 应用层解决
-- select * from student limit (3-1)*3,3  错误的-- 找到成绩及格的总分数排名第二的  select sid,sum(score) from sc where score>=60 group by sid  order by sum(score) desc limit 1,1
http://www.lryc.cn/news/406948.html

相关文章:

  • Spring源码-BeanFactory类关系层级
  • Electron 结合 Selenium + chromedriver 驱动服务实现浏览器多开
  • 手持式气象检测设备:便携科技,气象探测
  • shell 发送邮件脚本(免密)
  • Web动画(lottie篇)
  • 昇思25天学习打卡营第20天|CV-ResNet50图像分类
  • grep: /etc/mysql/my.cnf: 没有那个文件或目录
  • 养猫好物|宠物空气净化器是不是智商税?靠谱猫毛空气净化器推荐
  • 【CPS出版】2024年智能计算与数据分析国际学术会议(ICDA 2024,9月6日-8)
  • AutoGen框架革新:解锁新闻稿写作的新境界
  • 数据结构之队列详解
  • [渗透测试] 反序列化漏洞
  • C++ 类型转换 包括C风格的转换、static_cast、const_cast、reinterpret_cast、dynamic_cast、模板特化等
  • 等保通过标准
  • reduceByKey 函数详解
  • CSI-RS在信道中传输的过程
  • 建造者模式(Builder Pattern)工作原理
  • Ubuntu22.04安装Go语言的几种方式
  • Typora笔记上传到CSDN
  • Modbus转BACnet/IP网关BA100-配硬件说明
  • DjangoRF实战-2-apps-users
  • java面试题,有synchronized锁,threadlocal、数据可以设置默认值、把redis中的json转为对象
  • Apache Spark:深度解析
  • 使用umi作为模板如何实现权限管理
  • 系统架构设计师教程 第4章 信息安全技术基础知识-4.1 信息安全基础知识-解读
  • 【Rust光年纪】探索Rust游戏开发世界:六款引人注目的游戏引擎与框架
  • 从数据时代到智能时代,星环科技信雅达联合发布金融全栈解决方案
  • 自定义维度映射:Kylin Cube设计的高级玩法
  • c17 新特性 字面量,变量,函数,隐藏转换等
  • git操作的一些备忘录