plsql query statement reference

insert

==================插入单条数据=========================
insert into table_1 values(280,6,2000,3000,4000,5000,6000);
===============union 插入多条数据==================
insert into table_1 
select 50,'公关部','taiwan' from dual
union
select 60,'研发部','japan'  from dual
union
select 70,'培训部','uk' from dual
==================插入一个查询结果集==========================
insert into table_1 select 条件 from table_2;
==================向表中插入一个常量结果集=====================
INSERT INTO table_1 SELECT 's100106','卢俊义','男',23,
 TO_DATE('2009-8-9 08:00:10','YYYY-MM-DD HH24:MI:SS'),'1001'
FROM DUAL; 

select ... from dualThe dual table has only one row and one column in the system, which is used for the syntactic integrity of select ... from

Back up

1. 备份表
create table [备份名] as select * from [表名];
/*根据结果集创建一个新表*/
2. 恢复表
truncate table org_group;
insert into org_group select * from [备份名] ;

Deduplication

delete from table_1 a where rowid>(select min(rowid) from table_1 b where a.字段名1=b.字段名1)
/*根据字段名1删除重复数据*/
 SELECT DISTINCT 字段名 FROM table_name; /*删除重复行*/

The ROWID pseudo-column returns the physical address of the row, and the ROWID value can uniquely identify a row in the table.
ROWNUM identifies the order of the rows in the query results.

Count the number of the same data

select 字段名,count(1) from table_1 group by 字段名

Query result of joining two tables

Internal connection: the equivalent connection.

##简易写法
select a.字段名1,a.字段名2,d.字段名3 from table_1 a,table_2 d 
where a.字段名4=d.字段名4 and 筛选条件
===========sql/92标准写法(推荐),INNER 可以省略==================
SELECT a.字段名1,a.字段名2,d.字段名3 
FROM table_1 a INNER JOIN table_2 d ON a.字段名4=d.字段名4
WHERE 筛选条件

Left outer join: add the unmatched data in the main table on the basis of the inner join
Right outer join: add the mismatched data of the connected table on the basis of the inner join
(+): Oracle-specific connector, in the condition Appears in the left refers to the right outer join, appears on the right refers to the left outer
join.

##简易写法
select  a.字段名1,a.字段名2,d.字段名3 from table_1 a,table_2 d  where a.字段名4(+)=d.字段名4
==============sql/92标准写法(推荐),outer 可以省略==================
select  a.字段名1,a.字段名2,d.字段名3 from table_1 a right outer join table_2 d on a.字段名4(+)=d.字段名4

Nested query

SELECT statements can appear within SELECT, UPDATE, and DELETE statements. The result of the internal SELECT statement can be used as part of the conditional clause in the external statement, or as a temporary table for external queries; including exists, in, any, all, etc.

#单行单行单列子查询
select 字段名1,字段名2 from table_1  
where 字段名3 比较运算符 (select 字段 from table_2 where 条件 )

Comparison operators: =,>, <,> =, <=, <>, etc.
All: The condition is only established when all its data meets the condition.
Any: As long as one piece of data meets the condition, the condition holds

=ANY:表示与子查询中的每个元素进行比较,功能与IN类似(然而<>ANY不等价于NOT IN)
>ANY:比子查询中返回结果的最小的要大(还包含了>=ANY)
<ANY:比子查询中返回结果的最大的要小(还包含了<=ANY)

ALL has the following three forms of use:

ALL操作符有以下三种用法:
<>ALL:等价于NOT IN(但是=ALL并不等价于IN)
>ALL:比子查询中最大的值还要大(还包含了>=ALL)
<ALL:比子查询中最小的值还要小(还包含了<=ALL)

example:

select 字段名1,字段名2 from table_1  
where 字段名3 比较运算符 any(select 字段 from table_2 where 条件 )

oracle function

Character function:
Number function:
Date function:

ADD_MONTHS (d, n), add the specified number of months n to a certain date d, and return the new date after calculation. d is the date and n is the number of months to add.

LAST_DAY (d), returns the last day of the month of the specified date.

ROUND (d [, fmt]), returns a rounded date value in the format of fmt, d is the date, and fmt is the format model. The default fmt is DDD, which is the day of the month.
TRUNC (d [, fmt]), without rounding the date, directly intercepted to the first day of the corresponding format

EXTRACT (fmt FROM d), extract a specific part of the date. fmt is: YEAR, MONTH, DAY, HOUR, MINUTE, SECOND.

Conversion function:

TO_CHAR (d | n [, fmt]) converts date and number to a specified format string. fmt is a format string. Use double quotes to refer to unformatted characters.
TO_DATE (x [, fmt]) converts a string in fmt format to a date type.
TO_NUMBER (x [, fmt]) converts a string to Convert fmt format to a number

Aggregate function:

Published 10 original articles · Likes0 · Visits 959

Guess you like

Origin blog.csdn.net/weixin_43572702/article/details/105409343