数据库复习

select 查询

字段别名用 as (可以为中文) 例如 select ''

distinct 关键字 去重复值 例如select distinct deptno from test

where 条件过滤

and or 和 not运算符 and同时成立  or有一个成立就可以了 优先级and>or>not不符合(!)

in 匹配多个值 select 1 where 1 in (3,2)

Between:  SELECT * FROM Websites

WHERE alexa BETWEEN 1 AND 20; >=1 <=20

like搜索匹配的字符串: select * from test where name like '王%' 查询到 王111111  %匹配任意长度的字符串 —表示单一的字符 select * from test where name like '_王' 查询到1王而不会查询到11王

order by排序 默认是升序 从低到高 可以加关键字 desc降序 例如select name,salary from test order by salary desc;

单行注释-- 多行注释 /* */

null 空值 select 1 where '' is null  ->no 我们一般来说把不知道的值设置为null

not in的坑 not in表示不在集合中 not in 不能对空值进行判断

update更新一个或多个字段 update 表名 set 字段名 where 条件

例如 update test set name=1 where name='zs'

update中使用子查询 select * from test where deptno in (select deptno from depart where managerno=2 or managerno=3)

Delete删除记录 truncate table == delete from table  效率不同 truncate比delete高很多,把这个分配空间直接删除

Delete中使用子查询  从员工表中删除所有在二楼办公的员工

delete from test where deptno in (select deptno from test where loc like '二楼')

传统的多表连接方式

select name,dname,departments.deptno from employees,departments where employess.deptno=departments.deptno

一般来说,我们可以使用别名

select name,dname,d.deptno from employees e,departments d where e.deptno=d.deptno

inner join内连接 select name,dname,e.deptno from employees e inner join departments d on e.deptno=d.deptno 就是把这个俩个表连接在一起,好查询

self join 自连接 就是表自己连接自己 奥秘就是一个表使用不同的别名进行连接,别名好像是表的实体化一样,化成多个表进行连接

select t1.name,t2.empno,t1.empno from test t1 join test t2 on t1.name=t2.name and t1.empno<>t2.empno 这样就能找到 员工名字相同但是员工号不同的记录

outer join 外连接  

select * from A inner join B on A.id = B.id

select * from A inner join B on A.id = B.id

select * from A inner join B on A.id = B.id

cross join 交叉连接

select * from a cross join b

select * from a full join b on a.id = b.id

union集合联合

select empno,deptno from employees union select managerno,deptno from departments

加到一张表中,重复的只加一次

select empno,deptno from employees union all select managerno,deptno from departments

重复的也会加

select empno,deptno from employees  intersect select managerno,deptno from departments

俩张表中交互的内容

常用的分组函数

max()最大 min()最小 avg() 平均 sum()汇总 count(*)记录数

group by 分组

select deptno 部门,avg(salary) as 平均工资 from employees group by deptno 按部门分类 这里有一个注意点当跟order by 结合起用 顺序不能出问题现有group by 然后有order by

having过滤分组 在groupby后面的专门用来修饰group by

子查询 就是用一条数据实现多条数据的查询  查询入职时间比李四早的员工 select name from test where date<(select date from test where name='李四')

IN运算符中的子查询  查询所有在二楼办公的员工的姓名 select empno,name from employess where deptno in (select deptno from departments where depart loc='二楼')

子查询和连接 select empno,name from employess join departments  on employees.deptno=departments.deptno where loc='二楼' 效果也是一样的

all关键字 查询比所有1号部门所有员工工资高的员工

select name from employees where salary > all (select salary from employees where deptno=1);

ANY关键字

查询比2号部门任一员工工资低的员工

select name from test where salary < any (select salary from test where deptno=2)

其次in都可以修改为any

相关子查询 就是在子查询中使用外部查询的值

查询出比自己部门平均工资高的员工

select name,deptno,salary from test t where salary>(select avg(salary) from test where deptno=e.deptno)

变蓝色的就是外部查询 括号里面,绿色的就是子查询

EXISTS运算符

EXISTS 运算符用于判断查询子句是否有记录,如果有一条或多条记录存在返回 True,否则返回 False。

有时用来替代in 执行效率不同 如果子查询返回来的记录比较多,使用exists 如果主记录比较多,那就使用in

SELECT column_name(s) FROM table_name WHERE EXISTS (SELECT column_name FROM table_name WHERE condition);

select子句中的子查询

select name,salary,deptno,(select avg(salary) from test where deptno=e.deptno) 部门平均工资 

        from test t

from子句中的子查询

select * from (

        select name,salary,deptno,(select avg(salary) from test where deptno=e.deptno) 部门平均工资 

        from test t

)t2 order by salary

PARTITION BY 分区函数  这个函数跟group by有相似之处,但是功能要比group by厉害很多

写法:over(partition by cno order by degree )

SELECT    *

FROM    (select sno,cno,degree,

          rank()over(partition by cno order by degree desc) mm

          from score)

where mm = 1;

case where 表达式

select empno,deptno,

-> case deptno

->when 1 then '开发部'

->when 2 then '测试部'

-> when 3 then '销售部'

-> else '其他部门' end deptname

-> from employees

cte共用表达式

with 表名(自己定)as

    (

    select * from test where id in (select name from test2)

)   //结果集

select * from 表明

view视图

在 SQL 中,视图(View)是一个虚拟表,它由一个查询定义而成,并且可以像真实的表一样进行查询操作。视图并不实际存储数据,而是根据定义时指定的查询结果动态生成。

视图可以简化复杂的查询操作,并提供了一种安全机制,可以隐藏底层数据结构和敏感信息。通过创建视图,用户可以以一种更简单、更易理解的方式访问数据库中的数据。

创建视图

CREATE VIEW high_salary_employees AS

SELECT name, position, salary

FROM employees

WHERE salary >= 5000;

SELECT * FROM high_salary_employees;

查看视图

SHOW COLUMNS FROM high_salary_employees;

修改视图

ALTER VIEW high_salary_employees AS

SELECT name, position, salary

FROM employees

WHERE salary >= 6000;

更新视图数据

UPDATE employees

SET salary = 5500

WHERE employee_id = 123;

删除视图

DROP VIEW high_salary_employees;

注释注入

就是原本是select * from test where name='zs' and password='123';

一个完整的句子,但是前台如果输入zs';# 那么就会变成

select * from test where name='zs';#' and password='123';它会把查询到的所有句子返回

也可以select * from test where name='zs' and password=123 or 1=1;

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/2071.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Windows上查看服务器上tensorboad内容

文章目录 前言一、SSH的设置二、tensorboard命令 前言 本篇文章是针对于局域网内的服务器的tensorboard可视化&#xff0c;由于设置方式稍微有点复杂&#xff0c;导致我每次隔了一段时间之后&#xff0c;就不知道该怎么查看tensorboard了&#xff0c;每次都要百度搜一大堆资料…

SpringCloud系列(十六)[分布式搜索引擎篇] - DSL 查询及相关性算分的学习 (部分)

在SpringCloud系列&#xff08;十五&#xff09;[分布式搜索引擎篇] - 结合实际应用场景学习并使用 RestClient 客户端 API这篇文章中我们已经对 RestClient 有了初步的了解, 并且已经将一些数据进行了存储, 但是这并不是我们学习 ElasticSearch 的目的, ElasticSearch 最擅长的…

SQL中的where语句的使用

WHERE语句用于在SQL查询中过滤行&#xff0c;只返回满足特定条件的行。下面是一些常用的WHERE语句的例子&#xff0c;假设有三个表&#xff1a;users&#xff0c;products和orders。 1. 简单的WHERE子句 SELECT * FROM users WHERE age > 18; 这条语句将从users表中选择所…

物业管理微信小程序的设计与开发

1.物业管理微信小程序实现的功能 该微信小程序包含小程序端&#xff0c;后台管理端以及后端。 小程序端提供给业主使用&#xff0c;实现的功能模块有公告通知、访客预约、车位申请、装修申请、一键报修、报修单、意见反馈、缴费通知、一键求助、个人信息管理&#xff1b; 后台…

arcgis建筑物平均高度

主要用到相交和属性表的汇总功能。 路网 建筑物栋 相交结果 右键&#xff0c;bh列汇总 原始块有392&#xff0c;这里只有389&#xff0c;说明有的地块没有建筑&#xff0c;所以应该将表连接到原始街区上检查是否合理&#xff0c;以及随机验证一个结果是否正确。 连接结果&…

使用matlab里的集成树进行数据回归预测

当使用MATLAB时&#xff0c;您可以使用集成学习方法中的决策树来进行数据回归预测。决策树回归是一种基于树状结构的机器学习算法&#xff0c;它通过对训练数据进行分层次的决策来进行预测连续值的输出。 MATLAB提供了一个称为RegressionTree的集成树回归器。以下是一个使用MA…

无涯教程-Javascript - 变量声明

编程语言的最基本特征之一是它支持的数据类型&#xff0c;这些是可以用编程语言表示和操作的值的类型。 JavaScript允许您使用三种原始数据类型- 数字(Numbers)类型 - 如123、120.50等 字符串(Strings)类型 - 如"hello would"等 布尔值(Boolean)类…

中高级前端必须掌握的package.json最新最全指南

前言 package.json 是一个用于描述和配置项目的重要文件&#xff0c;其中包含了许多字段和选项&#xff0c;可以影响项目的构建、依赖管理、脚本执行等方面。了解这些字段可以帮助开发者更好地理解和控制项目的行为。 package.json对于大部分前端开发者来说&#xff0c;知道d…

spring boot maven 手动打入外部jar包依赖

有时候拿到第三方sdk是&#xff0c;以前都放在项目的某个目录下&#xff0c;然后通过项目路径去引入非常麻烦&#xff0c;最近找到了一个方法&#xff0c;可以手动将外部的jar包导入到本地的maven仓库中&#xff0c;这样你就可以像其他依赖一样正常使用了。 命令如下 mvn inst…

Spring6.0 源码部署

环境依赖 Git JDK17 Gradle&#xff08;版本号需要和Spring源码中的版本一致&#xff09; 源码下载 官网地址 源码配置修改 maven { url "https://maven.aliyun.com/repository/central" }gradle-wrapper.properties #distributionUrlhttps\://services.gradle…

无虚拟 DOM 版 Vue 进行到哪一步了?

前言 就在一年前的 Vue Conf 2022&#xff0c;尤雨溪向大家分享了一个非常令人期待的新模式&#xff1a;无虚拟 DOM 模式&#xff01; 我看了回放之后非常兴奋&#xff0c;感觉这是个非常牛逼的新 feature&#xff0c;鉴于可能会有部分人还不知道或者还没听过什么是 Vue 无虚…

离线安装Elasticsearch7.15.1集群(使用内置jdk)

离线安装Elasticsearch7.15.1集群(使用内置jdk) 背景&#xff1a; 以192.168.50.210、192.168.50.211、192.168.50.212这三台机器为例&#xff0c;进行相关的配置 而我本地的jdk是1.8的&#xff0c;已经不符合要求了。但项目中没有那么高版本的jdk&#xff0c;也只想用1.8版本…

Excel-公式VLOOKUP 使用方法-小记

个人愚见 表示 MongoDB列中的任意一条数据 在 MySQL列 精确查找 和MongoDB列 中一模一样的数据&#xff0c;有的话返回MongoDB列数据&#xff0c;没有话返回#N/A 官方解释

【Web安全】小白怎么快速挖到第一个漏洞,src漏洞挖掘经验分享,绝对干货!

src漏洞挖掘经验分享 – 掌控安全以恒 一、公益src 公益src是一个白帽子提交随机发现的漏洞的品台&#xff0c;我们可以把我们随机发现或者是主动寻找到的漏洞在漏洞盒子进行提交。 在挖掘src的时候不能越红线&#xff0c;一般情况下遇到SQL注入 只获取数据库名字以证明漏洞的…

myAgv的slam算法学习以及动态避障下篇

引言 在之前的一篇文章中有提到购入了一台myAGV&#xff0c;以树莓派4B为控制核心的移动机器人。上篇文章中向大家介绍了myAGV如何实现建图、导航以及静态避障&#xff0c;但我们深知&#xff0c;这只是机器人自主导航能力的基础。在实际应用场景中&#xff0c;机器人需要面对复…

Flask入门:flask run运行入口函数

背景&#xff1a; 这两天在看后端代码覆盖率平台代码的时候&#xff0c;发现启动服务只需要执行flask run命令即可。但是找了半天都没有看到工程中Flask app实例对象是在哪里创建的。工程中定义了一个create_app()函数&#xff0c;可是没有看到调用它的地方。带着疑惑&#xf…

2023牛客暑期多校训练营1

2023牛客暑期多校训练营1 D-Chocolate 题意 ​ 二人博弈&#xff0c;每局给出一个 n m nm nm的巧克力&#xff0c;每次操作可以选择一个点 ( x , y ) (x,y) (x,y)然后拿走所有 ( i ≤ x & & j ≤ y ) (i \leq x \&\&j\leq y) (i≤x&&j≤y)的巧克力…

创建 CephFS 文件系统 MDS 接口(短暂的分别是为了更好的再见)

文章目录 一、Ceph 简介二、Ceph 特点三、创建 CephFS 文件系统 MDS 接口四、 创建 Ceph 块存储系统 RBD 接口五、 创建 Ceph 对象存储系统 RGW 接口1、对象存储概念2、创建 RGW 接口3、将生成的证书合并为pem OSD 故障模拟与恢复1、模拟 OSD 故障2、将坏掉的 osd 踢出集群 一、…

【react】react18的学习(十一)– 底层原理(一)之 diff 算法

diff算法、fiber链表 步骤&#xff1a;&#xff08;追求多复用&#xff0c;快渲染&#xff09; 首次渲染&#xff0c;缓存虚拟dom或fiber链表&#xff08;17及以后&#xff09;&#xff1b; 组件更新&#xff0c;将新生成的虚拟dom与已有的真实dom的fiber链表对比&#xff1b…

【C语言+sqlite3 API接口】实现水果超市

实验内容&#xff1a; 假如我家开了个水果超市&#xff0c;有以下水果&#xff0c;想实现自动化管理&#xff0c;扫描二维码就能知道当前的水果状态&#xff0c;进货几天了&#xff0c; 好久需要再次进货&#xff0c;那些水果畅销&#xff0c;那些水果不畅销&#xff0c;那些水…