/*
database--
1. Follow the SQL standard
2 different manufacturers, different database products, but have their own dialect
3. Use their own dialect, can do the same thing
4.oracle security level is higher, mysql open source
* /
select 1 + 1 - Oracl given equal, output = 2 in mysql
/ *
Dual: Oracle in the virtual table, a table, a syntax structure is mainly used to afford
*/
select 1+1 from dual; select * from dual;
selsct 1 from emp;
- Direct write a constant to efficiently ward off evil *
select count(1) from emp; select count(*) from emp;
/ *
Alias query: use as a keyword, you can omit
the alias can not have special characters or keywords, if you double quotes
* /
select ename name, sal emp wages from; select ename "name", sal from emp wages;
/ *
Remove duplicate data distinct
multi-column to remove duplicate: Each column can be considered as doing enough repeat
* /
- Query table
select * from ACCQRYBYPKS;
- Add Use Case
insert into ACCQRYBYPKS (PK_ACCASOA, PK_ACCCHART) values (02, 'test class');
- single de-duplication
select distinct PK_ACCASOA from ACCQRYBYPKS;
- remove duplicate multiple columns
select distinct PK_ACCASOA,PK_ACCCHART from ACCQRYBYPKS;
/ *
Fuzzy query: like
with a character
matches any single character
* /
insert into ACCQRYBYPKS (PK_ACCASOA, PK_ACCCHART) values (02, ' test class'); INSERT INTO EPM () values () SELECT * from EMP; INSERT INTO EMP (ID, NAME, the JOB, the PLANT, MONTH, YEAR) values ( '01', 'li three', 'programmer', 'Beijing', '500', '6000') INSERT INTO emp (ID, NAME, JOB, the PLANT, MONTH, YEAR) values ('01 ',' Rees '' programmer ',' Shijiazhuang ',' 500 ',' 6000 ') INSERT INTO emp (ID, NAME, JOB, the PLANT, MONTH, YEAR) values ('01', 'I love', 'programmer' 'Henan', '500', '6000') INSERT INTO emp (ID, NAME, JOB, the PLANT, MONTH, YEAR) values ('01 ',' crack ',' programmer ',' Shanghai ',' 500 ',' 6000 ')
- Query third character is the employee name employee information 0
select * from emp where name like 'l%'
- Query far in the name of workers, contains% of employee information
- transfer character
select * from emp where name like '%\%%' escape '\';
/ *
Sorting: order by
ascending: asc ascend
descending: desc descend
Sort note null question: NULLS First | Last
* /
- Query employee information, sorted by descending id
select * from emp order by id desc nulls last;
- Date Functions
- inquiry today's date
select sysdate from dual;
- Query today's date three months after the
select add_maonths(sysdate,3) from dual;
- 3 days after the date of inquiry
selet sysdate + 3 from dual;
- turn switch conversion function value date character numeric characters
- numerical characters turn to_number (str)
select 100 + '10 'from dual; --110 a default conversion select 100 + to_number ('10') from dual; --110
- the date of conversion characters to_char ()
select to_char(sysdate,'yyyy-mm-dd hh:mi:ss') from dual;
- only in want
TO_CHAR SELECT (SYSDATE, 'YYYY') from Dual; SELECT TO_CHAR (SYSDATE, 'D') from Dual; - represents a type in which the first few days select to_char (sysdate, 'dd' ) from dual; - represents a the first few days of the month select to_char (sysdate, 'ddd' ) from dual; - represents the first day of the year the SELECT the TO_CHAR (sysdate, 'Day') from Dual; --monday the SELECT the TO_CHAR (sysdate, 'by ') from dual; --mon week shorthand
- The character transfer date
select to date('2019-08-27','yyyy-mm-dd') from dual;
- Query 1981 - - 1985 recruits converts character information to_date
select * from emp where (出生年) between to_date('1981','yyyy') and to_date('1985','yyyy');
/ *
Conditional Expression
case field
when value 1 then the value
when value 2 then the value
else
the default values
End
case..when mysql and oracle general wording may be used in
specific wording: decode (field, if1, hen1, if2, then2 , else)
* /
- to the table to take a Chinese name names
SELECT Case ename When '-Mar' the then 'Bei' When 'All' the then 'Guan' the else 'passerby' End "Chinese name" from EMP;
/ *
Packet group by the expression
conditions select packets from the operation table group by the grouping condition having the packet over conditions after
The order to write SQL:
select..from..where..group by .. ..ordey by the HAVING
SQL execution order:
from..where..group by the HAVING ..select..ordey by ..
* /
- group statistics the average wage in all sectors, to identify sectors is greater than the average wage of two thousand
select deptno,avg(sa1) from emp group by deptno;
- filtering at greater than 2000
select deptno,avg(sal) from emp group by deptno having avg(sal)>2000;