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()
函数返回数值列的平均值
SUM()
函数返回数值列的总和搭配GROUP BY,经常用于统计某个分组得总和
COUNT()
函数返回 与指定条件匹配的行
进阶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 '%境外||%';