sql basic knowledge (notes) (two)

**

sql basic knowledge (notes) (two)

**

Continuing the basic knowledge of
SQL (notes) (1)

**

function:

**

distinct:去除重复数据 distinct empno
trim:去掉字符串两头空格 trim(job)
ltrim:去掉字符串左边空格
rtrim:去掉字符串右边空格
ceil:向上取整 ceil(97.2)--98 ceil(-35.3)-- -35
floor:向下取整 floor(97.2)--97  floor(-35.3)-- -36
ABS:取绝对值 ABS(5)--5  ABS(-6)--6
last_day:获取给定时间月份的最后一天 last_day(sysdate)--20200229  last_day(to_date('20201001','YYYYMMDD'))--20201031
upper:小写转大写 upper('abc')--'ABC'
lower:大小转小写
length:返回参数字符长度 length('ABC')--3 length(列名)--返回列里每个字符串的长度
||:拼接符 'AB'||'CD'--'ABCD'
to_char:时间to_char to_char(sysdate,'YYYYMMDD')--'20200206'.也可以处理数据,自己搜索学习
to_char(数字,格式):便于阅读
to_date:字符串转化为时间格式 to_date('2020-02-05','YYYY-MM-DD')--时间格式 2020-02-05
months_between:2个参数,都是时间,返回的结果是参数1-参数2的月份数 months_between(时间1,时间2)--时间1-时间2对应的月份数
add_months:2个参数,增加月份 add_months(时间,数字) 给时间增加n个月.数字可正可负
replace:3个参数 repalce(参数1,参数2,参数3):将参数1中的参数2替换为参数3.repalce(' A B C  D',' ','');将字符串中的空格去掉
substr:
2个参数:substr(参数1,参数2):截取参数1,从参数2位开始到最后 参数2位正时,是从前往后截取,直到最后. 参数2位负数时,是从后往前的参数2位到最后 substr('ABCDE',2)--BCDE  substr('ABCDE',-2)--DE
3个参数:substr(参数1,参数2,参数3):截取参数1,从参数2位开始,截取参数3. substr('ABCDE',3,2):--CD 截取ABCDE,从第三位开始截取,截取2位;
round:
1个参数:
round(12.56)--13
round(12.34)--12
2个参数:
round(12.56,1):小数点后保留1,且有四舍五入 12.6
round(12.567,2):12.57
round(12.56,-1):小数点钱第一位置为0,且有四舍五入 10
round(sysdate,'D'):最近的周日
round(sysdate,'MM'):最近的月第一天 
round(sysdate,'Q'):最近的季度第一天
round(sysdate,'Y'):最近的年第一天
trunc:
1个参数:
trunc(12.56)--12保留整数且没有四舍五入
trunc(12.34)--12
trunc(sysdate):不带时分秒的时间
2个参数:
trunc(12.56,1):12.5 小数点后保留1,无四舍五入
trunc(12.567,2):12.56
trunc(12.56,-1):10
trunc(12.56,-2):0
trunc(sysdate,'D'):当周第一天
trunc(sysdate,'MM'):当月第一天
trunc(sysdate,'Q'):当季度第一天
trunc(sysdate,'Y'):当年第一天

instr
2个参数:instr(参数1,参数2):返回位置,参数2在参数1中第一次出现的位置 select instr('ABCDEC','C') from dual;--3
4个参数:instr(参数1,参数2,参数3,参数4):从参数1中的参数3位开始找,参数2在参数1中出现参数4次的位置;select instr('AAAAAAA','A',3,2) from dual;--4
nvl 2个参数 nvl(参数1,参数2):当参数1为空时,返回参数2;--select nvl('',1) from dual; --1
nvl2 3个参数: nvl2(参数1,参数2,参数3):当参数1的值为空时返回参数3,否则返回参数2
select nvl2('',1,2) from dual;--2
select nvl2('A',1,2) from dual;--1
decode
decode(参数1,参数2,参数3,参数4,参数5,参数6):当参数1的值等于参数2,返回参数3,当参数1的值等于参数4时返回参数5,其余时候返回参数6
select decode(3,1,'A',2,'B',3,'C','D') from dual;--C

**

Related query

**
Basic associated query syntax:
Basic syntax:

select * from1,2,3 where1.列名=2.列名 and2.列名=3.列名;

Associated column selection: When the relationship is transferred, select the columns with the same business meaning for association.-Note
left connection :
-Display all the columns of at most two tables, the left table is in the front, and the right table is in the back. Display all data in the left table and the right table The upper part can be linked to display the data in the right table, otherwise the right table part is displayed as empty;
basic syntax:

select * from1 left join2 on1.列名=2.列名;
select * from1,2 where1.列名=2.列名(+);

Right connection :
-Displays all the columns of the two tables at most, the left table is in the front, the right table is in the back, all the data of the right table is displayed, the part of the left table can be associated, the data of the left table is displayed, otherwise the left table is displayed as empty ;
Basic syntax:

select * from1 right join2 on1.列名=2.列名;
select * from1,2 where1.列名(+)=2.列名;

Inner join:
functionally equivalent to the basic associated query
Basic syntax: select * from Table 1 inner join Table 2 on Table 1. Column name = Table 2. Column name;
outer join:-divided into left outer join and right outer join , Functionally equivalent to left and right connection
Basic syntax:

select * from1 left outer join2 on1.列名=2.列名;
select * from1 right outer join2 on1.列名=2.列名;

Full connection:
Functionally, it is a collection of left and right connections, displaying all the data in the left table and all the data in the right table, and the unrelated parts are filled in separately;
basic syntax:

select * from1 full join2 on1.列名=2.列名;

Natural connection:
Similar in function to basic association query, it will automatically find the columns with the same name in the two tables for association;
basic syntax:

select * from1 natural join2;

date_created和date_updated


The concept of sub-query sub-query: When filtering the query, our filter condition is that the column is equal to a certain value, and this value is directly given. If this value is not given directly, but the calculation logic and parameters are given, then Use a sql to get, and the obtained sql is a subquery
. Where can it be used:
1. As the content of the select;
2. As part of the where condition;
3. As part of having
4. As a result set (subquery as A table);

**

set

**
Collection:
union and union all, union has the function of deduplication and sorting (ascending order), when SQL optimization, if allowed, you can replace unoin with union all
intersection:
intersect takes the partial
difference of the intersection of two result sets:
minus, A minus B shows the part of A that removes the intersection of AB and
B minus A shows the part of that that removes the intersection of AB in B

Additions, deletions and modifications:
insert:
1. Insert data in all columns:

 insert into 表名(列名1,列名2,列名3) values(1,2,3);

Among them, the parentheses after the table name can be omitted. The value and the column name are one-to-one.
2. Insert data in some columns:

insert into 表名(1,3) values(1,3);

3. Table insert table insert into table name select * from table name;
if you want to insert some columns, you can write:

insert into 表名(1,2,3) select1,2,3 from 表名;

delete:

delete from 表名 where 条件;

--Delete data that meets the where condition, the where condition part can be omitted, that is, the entire table is deleted;
truncate: delete the entire table data syntax:

truncate table 表名;

The difference between delete and truncate:
1.delete is a DML statement and needs to be submitted; truncate is a DDL statement and does not need to be submitted;
2.delete can delete the entire table data or part of the data; truncate only targets the entire table data;
3. When there is a large amount of data, delete efficiency is low (write a large amount of logs for data recovery), and truncate is very efficient (the amount of logs is small, and it is difficult to recover data);

update:
basic syntax:

update 表名 set=where 条件;

-Change the value of the column in the table that meets the where condition data to the given value; if the where condition is not written, the entire column is updated;

Union table update:

update 表名 set=where 条件
exists(子查询);

exists: If the query has a value, it returns true, and if there is no value, it returns false.

update 表名 set=where 条件
andin(子查询);
merge into 表名 a
using 表名或者查询语句 b
on(关联条件)
when matched then
update set
a.列名1=b.列名1,
a.列名2=b.列名2
when not matched then
insert values(a.1,a.2);

1. After using, it can be a table name or a filtering query statement;
2. When macthed and when not matched can be only one, and you don’t need to have all of them;
3. When inserting, you can insert some columns;-Find notes
4 When updating, you can add conditional updates (only update part of the data);-Find notes
5. When it comes to partition tables, the partition key in merge cannot be updated;

-End of DML part
ddl talk about next part

Guess you like

Origin blog.csdn.net/yang_z_1/article/details/111878696