oracle 基础语法复习记录

news/2025/2/6 14:54:08 标签: oracle, 学习, 数据库

Oracle SQL基础

 学习范围

  • 学习SQL基础语法

    • 掌握SELECTINSERTUPDATEDELETE等基本操作。

    • 熟悉WHEREGROUP BYORDER BYHAVING等子句。

  • 理解表连接

    • 学习INNER JOINLEFT JOINRIGHT JOINFULL OUTER JOIN等连接方式。

  • 掌握聚合函数

    • 熟悉SUMCOUNTAVGMINMAX等函数的使用

进阶学习

  • 子查询

    • 学习如何在SELECTFROMWHERE中使用子查询。

    • 理解相关子查询和非相关子查询的区别。

  • 窗口函数

    • 学习ROW_NUMBER()RANK()DENSE_RANK()LEAD()LAG()等窗口函数。

    • 掌握OVER子句的使用,尤其是PARTITION BYORDER BY

  • 递归查询

    • 学习使用WITH递归查询(CTE,Common Table Expressions)处理层次结构数据。

  • 集合操作

    • 掌握UNIONUNION ALLINTERSECTMINUS等集合操作。

SQL基础语法

学习记录:

 select

查询客户表所有列

select * from tcustinfo ;

查询客户表指定列

select t.vc_custno,t.vc_customname,t.vc_identityno from tcustinfo t;

查询有效状态得客户指定列名

select t.vc_custno,t.vc_customname,t.vc_identityno from tcustinfo t where t.c_state='0';
查询有效客户数量

select count(1) from tcustinfo t where t.c_state='0';
查询有效客户证件类型

select distinct(t. c_identitytype) from tcustinfo t where t.c_state='0';

查询2024年1月1日起,有效客户数量

select count(1) from tcustinfo t where t.vc_opendate > 20240101 and  t.c_state='0';
 

拓展:where 过滤条件,不仅仅用于select查询,update,delete中同样适用。

以下运算符可用于WHERE条款,>, <, =, >=,>=,<>,BETWEEN,like, in等

ORDER BY关键字用于按升序asc或 降序 desc。

按开户时间进行降序排序 desc 
select vc_opendate from tcustinfo t order by t.vc_opendate   DESC  

WHERE子句可以包含一个或多个 AND运算符。AND运算符用于根据多个记录筛选记录 条件 ,

筛选出2024年开户,开户类型为机构得客户


select * from tcustinfo t where t.vc_opendate between 20240101 and  20241231 and  t.c_state='0' and t.vc_customname like '%机构%';

筛选出2024年开户,开户类型为:机构或者产品得客户

select * from tcustinfo t where t.vc_opendate between 20240101 and  20241231 and  t.c_state='0' and ( t.vc_customname like '%机构%' or  t.vc_customname like '%产品%');


not  不在,统计相反得运算符,可以和其他运算符搭配,比如  not in,not like,NOT BETWEEN 10 AND 60; not CustomerID50 也可以用  !> 来表示 (not <   也可以用!<)

select * from tcustinfo t where   not t.c_identitytype ='0';

select * from tcustinfo t where   t.c_identitytype !='0';
 

select * from tcustinfo t where t.vc_opendate > 20240101 and  t.c_state='0' and  t.vc_customname  not like '%机构%'
 

insert

INSERT INTO table_name
VALUES (value1, value2, value3, ...);

INSERT INTO Customers (CustomerName, City, Country)
VALUES ('Cardinal', 'Stavanger', 'Norway');

也可以同时插入多条语句

INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES
('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway'),
('Greasy Burger', 'Per Olsen', 'Gateveien 15', 'Sandnes', '4306', 'Norway'),
('Tasty Tee', 'Finn Egan', 'Streetroad 19B', 'Liverpool', 'L1 0AA', 'UK');

null not null

当无法使用比较运算符 例如 = 、 <或 <>,可以用 IS NULL IS NOT NULL来替代

查询vc_instreprname 为null得数据

select * from tcustinfo t where  t.vc_instreprname is null;

查询vc_instreprname 不为null得数据

select * from tcustinfo t where  t.vc_instreprname is not null;

update

 UPDATE语句用于修改表中的现有记录(注意!!要加where条件,否则会变更所有记录

UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;

delete

DELETE语句用于删除表中的现有记录。(注意!!要加where条件,否则会删除所有记录

DELETE FROM Customers WHERE CustomerName='Alfreds Futterkiste';

聚合函数

AVG()函数返回数值列的平均值

查询所有产品平均价格

SELECT AVG(Price) FROM Products; 

SUM()函数返回数值列的总和

搭配GROUP BY 经常用于统计某个分组得总和

统计不同投资人 个人0 机构1 产品2,得开户数量

select sum(1),t.c_custtype from tcustinfo t group by t.c_custtype

统计 不同投资人类型得 确认份额 并且按照 客户类型 倒叙


select sum(t.en_confirmshare),t.c_custtype from tconfirm t group by t.c_custtype order by  t.c_custtype desc 

COUNT()函数返回 与指定条件匹配的行

查询一共发行多少产品 
select count(1)  from tfundinfo t 

并按不同类型得产品分组统计,每个类型得产品多少数量

select count(1),c_type  from tfundinfo t  group by t.c_type
 

LIKE运算符

LIKE运算符用于 WHERE子句在列中搜索指定的模式,常用于模糊查询

有两个通配符经常与 LIKE操作员:

  •  百分号%表示零个、一个或多个字符
  •  下划线符号_表示一个单个字符

比如查出姓名带有“王”得投资人

select * from tcustinfo t where  t.vc_customname like '%王%';

再比如 ,查出名字中带有王X杰得投资人,中间字可以用下划线 —— 来表示。

select * from tcustinfo t where  t.vc_customname like '%王_杰%';
 

也可以搭配 and or 运算符查询,比如查出王或者姓毛得投资人

select * from tcustinfo t where  t.vc_customname like '%王%' or   t.vc_customname like '%毛%';
 

或者查询第二个字为妃得投资人


select * from tcustinfo t where  t.vc_customname like '_妃%'

%_ ,可与其他组合使用 通配符 查询我们想获得得数据

比如查询前面只有一个字,后面是明杰得投资人,比如X明杰

select * from tcustinfo t where  t.vc_customname like '_明杰'

再比如查询,香XX蛋 中间两个字符,hh 这是奇怪得搭配香菜煎蛋,笑不活了,谁造得啊


select * from tcustinfo t where  t.vc_customname like '香__蛋' 

IN运算符

IN运算符是多个 OR条件

select * from tcustinfo t where t.vc_custno in ('2638','2648','2678')
 

查询所有未下单得客户 

SELECT * FROM Customers
WHERE CustomerID NOT IN (SELECT CustomerID FROM Orders);

BETWEEN运算符

选择给定范围内的值。值可以是数字、文本或日期。

选择价格在10到20之间的所有产品:

SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;

SELECT * FROM Products
WHERE Price NOT BETWEEN 10 AND 20;

SQL别名

SQL别名用于为表或表中的列提供临时名称。

别名通常用于使列名更可读,易懂。

别名仅在该查询的持续时间内存在。

使用创建别名AS关键字

select t.vc_acconame as 客户姓名,t.vc_tradeacco  客户交易账户  from taccoinfo  t

进阶sql

从 taccoinfo 表中筛选出 vc_address 包含 || 但不包含 境外|| 的记录,然后将这些记录的 vc_tradeacco 和 vc_address 拼接成一个字符串,并统计符合条件的记录数

SELECT LISTAGG('交易账号:' || vc_tradeacco || ' 地址:' || vc_address, ', ') WITHIN GROUP (ORDER BY vc_tradeacco) AS RETURNMSG,
       COUNT(1) AS count_num
FROM taccoinfo t
WHERE t.vc_address LIKE '%||%'
  AND t.vc_address NOT LIKE '%境外||%';


 


http://www.niftyadmin.cn/n/5843094.html

相关文章

如何在页面中弹出菜单

文章目录 1 概念介绍2 使用方法3 示例代码 我们在上一章回中介绍了Sliver综合示例相关的内容&#xff0c;本章回中将介绍PopupMenuButton组件.闲话休提&#xff0c;让我们一起Talk Flutter吧。 1 概念介绍 我们在本章回中介绍的PopupMenuButton组件位于AppBar右侧&#xff0c;…

Java进阶笔记(中级)

-----接Java进阶笔记&#xff08;初级&#xff09;----- 目录 集合多线程 集合 ArrayList 可以通过List来接收ArrayList对象&#xff08;因为ArrayList实现了List接口&#xff09; 方法&#xff1a;接口名 柄名 new 实现了接口的类(); PS: List list new ArrayList();遍历…

【Elasticsearch】filter聚合

在Elasticsearch中&#xff0c;Filter聚合是一种单桶聚合&#xff0c;用于根据特定的查询条件筛选文档&#xff0c;并对筛选后的文档集合进行进一步的聚合分析。它允许用户在执行聚合操作之前&#xff0c;先过滤出符合某些条件的文档&#xff0c;从而更精确地分析数据。 Filter…

ES6 const 使用总结

1. 声明不可变性 1.1 基本类型的不可变性 // 基本类型声明后不能修改 const name John; name Jane; // TypeError: Assignment to constant variableconst age 25; age 26; // TypeError: Assignment to constant variableconst isValid true; isValid false; // Ty…

java使用LibreOffice实现word转pdf

之前使用dom4j转换&#xff0c;依赖office&#xff1b; 网上还有Apache poi和itextpdf方式&#xff0c;尝试后发现复杂word&#xff0c;比如包含表格曲线等支持性不好。 最后发现 LibreOffice&#xff1a;不依赖office&#xff0c;免费&#xff0c;可跨平台 参考链接&#xf…

Ollama+deepseek+Docker+Open WebUI实现与AI聊天

1、下载并安装Ollama 官方网址&#xff1a;Ollama 安装好后&#xff0c;在命令行输入&#xff0c; ollama --version 返回以下信息&#xff0c;则表明安装成功&#xff0c; 2、 下载AI大模型 这里以deepseek-r1:1.5b模型为例&#xff0c; 在命令行中&#xff0c;执行&…

国防科大:双目标优化防止LLM灾难性遗忘

&#x1f4d6;标题&#xff1a;How to Complete Domain Tuning while Keeping General Ability in LLM: Adaptive Layer-wise and Element-wise Regularization &#x1f310;来源&#xff1a;arXiv, 2501.13669 &#x1f31f;摘要 &#x1f538;大型语言模型&#xff08;LLM…

产品经理的人工智能课 02 - 自然语言处理

产品经理的人工智能课 02 - 自然语言处理 1 自然语言处理是什么2 一个 NLP 算法的例子——n-gram 模型3 预处理与重要概念3.1 分词 Token3.2 词向量化表示与 Word2Vec 4 与大语言模型的交互过程参考链接 大语言模型&#xff08;Large Language Models, LLMs&#xff09;是自然语…