首页 > 其他分享 >4:查询

4:查询

时间:2023-01-15 22:01:54浏览次数:41  
标签:dept -- 查询 person employee where select

--去重--distinct
select distinct sname from student

--排序--order by(ASC升序[默认] DESC降序)
select distinct sage from student order by sage asc

--限量--limit
select distinct sage from student order by sage asc limit 17

-- 单表查询-全部
select * from person;

-- 部分
select name,age from person;

-- 指定列名
select name as '名字', age as '年龄' from person;

-- 条件查询
select * from person where phone='13545269582';
select * from person where phone !='13545269582';
select * from person where age >= 24;

-- between(包含)
select * from person where age between 22 and 25;

-- like模糊 _单字符 %所有
-- 以朱开头
select * from person where name like '朱%';
-- 以1结尾
select * from person where name like '%1';
-- 所有带朱字的人
select * from person where name like '%朱%';

-- in 其中一个
select * from person where phone in (12645825963,12645825967);

-- 判断 空 is null not null

select * from person where name is null;
select * from person where name is not null;

 

 

 

 

 

/*-- 多表查询
外键:就是把一张表的主键拿到另一张表中作为一个普通字段
*/

select id from dept where deptName='明教'
select * from employee where deptNo=3

-- 子查询


select * from employee where deptNO in (select id from dept where deptName='明教')

-- 查询每个部门的平均工资--部门只有序号
select deptNO,avg(salary) from employee group by deptNO

-- 每个部门和员工平均工资一起查询(显示部门名)
select dept.deptName,avg(employee.salary) from dept,employee where dept.id=employee.deptNO group by dept.deptName

-- 关联查询 on
-- inner join 内链接
-- 通常关联项on后面的就是主外键
select * from dept join employee on dept.id=employee.deptNO

-- 左联 left join
select * from employee left join dept on dept.id=employee.deptNO

-- 查看所有员工的部门
select employee.name,dept.deptName from employee left join dept on employee.deptNO=dept.id
-- 右链接

 

 

 

 

-- 聚合函数--group by
-- 年龄最大
select max(age) from person
-- 年龄最小
select min(age) from person
-- 平均年龄
select avg(age) from person
-- 一共多少人
select count(*) from person
-- 求所有年龄之和
select sum(age) from person

-- 求每个班级的平均年龄
select cls,avg(age) from person group by cls
-- 查询每个班级的最大年龄
select cls,max(age) from person group by cls

-- having 一般在聚合函数或group by 以后进行分组在进行条件筛选
-- 查询每个班级有多少人
select cls,count(*) from person group by cls
-- 查询班级人数小于3人
select cls,count(*) from person group by cls having count(*) < 3

-- order by 排序 默认升序asc 降序desc
select * from person order by age asc

select * from person order by age desc, phone asc

 

标签:dept,--,查询,person,employee,where,select
From: https://www.cnblogs.com/vip01/p/17054210.html

相关文章

  • 好的基因查询工具
    本文由简悦SimpRead转码,原文地址mp.weixin.qq.com我是谁?我在哪儿?我在查什么?在信息爆炸的时代,相信很多小伙伴在查文章时会因信息量太大而抓狂。今天带来一款设计简......
  • MySQL架构-sql查询执行流程-sql解析顺序
    参考:https://www.cnblogs.com/annsshadow/p/5037667.htmlMySQL架构sql查询执行流程sql解析顺序......
  • 车牌归属地查询
    广东省车牌归属地查询(粤)牌号地区粤A广州市粤B深圳市粤C珠海市粤D汕头市粤E佛山市粤F韶关市粤G湛江市粤H肇庆市粤J江门市粤K茂名市粤L惠州市粤M梅州市粤N......
  • 关于查询maven依赖排查的问题
    背景公司扫描服务依赖项的时候,发现服务中有引用了logback的包,因版本过低,需要升级才能修复风险。通过maven的DependencyAnalyzer工具,确实找到了一些,排掉后,扫描发现,还存......
  • 模拟表存在坏块,索引对坏块对应rowid查询的影响
    文档课题:模拟表存在坏块,索引对坏块对应rowid查询的影响.数据库:oracle19.12多租户测试结果:表存在坏块,若该表有索引,坏块对应的rowid可以查询;若没有索引便不能查询.1、测试数......
  • mybatis-plus 直接查询的数据对对象带嵌套的对象
    1.javaentity定义@Data@EqualsAndHashCode(callSuper=true)@Accessors(chain=true)@TableName(value="delta24_alarmrule",autoResultMap=true)publicclass......
  • 【数据库】PostgreSQL/PgSql-根据模式名和字段名查询有该字段的所有表信息【通过表元
    【数据库】PostgreSQL/PgSql-根据模式名和字段名查询有该字段的所有表信息【通过表元数据信息和函数实现】...哥们要飞于2022-08-2314:51:00发布304收藏文章标签:数......
  • linux 快速查询指令的用法
    --help几乎所有的指令,都可以使用--help选项进行查询。给命令使用--help选项,就会直接出现一段说明命令的文字。>date--help用法:date[选项]...[+格式]或:date[-u|-......
  • es查询慢原因排查
    前言经常会有人吐槽,Elasticsearch为什么查着查着突然就慢了?笔者总结了常见的一些导致查询慢的场景,供大家排查。Elasticsearch查询慢问题排查思路Elasticsearch的查询慢......
  • Elasticsearch中的refresh和flush操作指南(es数据写入但是查询不到问题)
    在今天的文章里,我们来主要介绍一下Elasticsearch的refresh及flush两种操作的区别。如果我们从字面的意思上讲,好像都是刷新的意思。但是在Elasticsearch中,这两种操......