Oracle SQL基础
学习范围
-
学习SQL基础语法
-
掌握
SELECT
、INSERT
、UPDATE
、DELETE
等基本操作。 -
熟悉
WHERE
、GROUP BY
、ORDER BY
、HAVING
等子句。
-
-
理解表连接:
-
学习
INNER JOIN
、LEFT JOIN
、RIGHT JOIN
、FULL OUTER JOIN
等连接方式。
-
-
掌握聚合函数:
-
熟悉
SUM
、COUNT
、AVG
、MIN
、MAX
等函数的使用
-
进阶学习
-
子查询:
-
学习如何在
SELECT
、FROM
、WHERE
中使用子查询。 -
理解相关子查询和非相关子查询的区别。
-
-
窗口函数:
-
学习
ROW_NUMBER()
、RANK()
、DENSE_RANK()
、LEAD()
、LAG()
等窗口函数。 -
掌握
OVER
子句的使用,尤其是PARTITION BY
和ORDER BY
。
-
-
递归查询:
-
学习使用
WITH
递归查询(CTE,Common Table Expressions)处理层次结构数据。
-
-
集合操作:
-
掌握
UNION
、UNION ALL
、INTERSECT
、MINUS
等集合操作。
-
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 CustomerID > 50 也可以用 !> 来表示 (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之间的所有产品:
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 '%境外||%';