The complete syntax rules of the Select statement are as follows:
select {*,column[alias],...}
from table
[where...] First execute the where statement to filter the original records
[group by...] Execute group by to group
[having...] Execute having filter grouping
[order by...] Then select the field values in select and
finally execute order by to sort
Example :
1 between...and...
select ename,sal from emp where sal between 800 and 1500;
Description: between...and... contains equal cases
2. The default date format of the processing date oracle is "DD-MON-RR", which is a two-digit date-month abbreviation (for Chinese Say is the full name) - two-digit year, as follows: July 6, 2017
select distinct * from user_role t where rolename in ('organization administrator','manager','tester') and t.operatetime >' 01-July-17';
select distinct * from user_role t where rolename in ('Organization Admin','Administrator','Tester') and t.operatetime between '01-July-17 'and '06-July-17 ' ;
3 .The fuzzy query
keyword 'like' is used as a fuzzy query, the wildcard '%' means zero or more characters;
the wildcard '_' means one character;
select distinct * from user_role t where rolename like '%admin%' ;
select distinct * from user_role t where rolename like 'manage_';
4. Sort
order by Normally, when data is retrieved, it is displayed in the order in which it was inserted. When it needs to be sorted and displayed as required, use keywords order by . Default ascending asc descending desc
select t.rolename ,t.operatetime from user_role t where t.rolename in ('Organization Manager','Administrator','Tester') and t.operatetime > '01-7 Month-17 ' order by t.operatetime desc ;
sort by multiple fields
select t.userroleid , t.rolename ,t.operatetime from user_role t where t.rolename in ('Organization Manager','Administrator','Tester') and t.operatetime > '01-July-17' order by t.operatetime desc ,t.userroleid desc ;
5. Upper and lower case sql function lower upper
select lower('AaCDEFG') a from dual
select upper('AaCdFG') a from dual
6 fetch substring substr
select substr('sssssshello ','10',5) from dual;
7. Take the character corresponding to the ASCII code value
select chr(65) from dual;//A
8. Find the ASCII code value of a character
select ascii('A') from dual ; //65
9. Rounding round
select round(24.5) from dual; //25
select round(24.5527,2) from dual;//24.55
select round(24.5527,-2) from dual; //0
select round(24.5527,-1) from dual;//20
10.to_char();
to_char
is used to convert numbers or dates into specific strings, to_char has two parameters,
the first parameter is the date or number that needs to be converted, and the
second parameter is a specific conversion format, for numbers , to_char has the following format specifications:
format control character meaning
9 represents a digit, if there is no digit in the digit, it will not be displayed, but the part after the decimal point will still be forced to display
0 to represent a digit, if there is no digit in the digit Then force to display 0
$ Display dollar sign
L Display local currency symbol
. Display decimal point
, display thousandth symbol
Date format
YYYY, YY represents four-digit, two-digit year
MM Numbered month
DD Numbered day
MON Month of the month Abbreviation, for Chinese month, it is full spelling (February)
DY Abbreviation of week, for Chinese week it is full name
HH24, HH12 12 hours or 24 hours
MI minutes
SS seconds
select to_char(sysdate,'YYYY-MM -DD MON DY HH24:MI:SS') from dual;
//2017-07-18 Tuesday, July 16:49:31
select to_char(sysdate,'YYYY-MM-DD HH24:MI:SS') from dual;
select to_char(sysdate,'YYYY/MM/DD HH24:MI :SS') from dual;
11.to_date
converts a string in a specific format into a date format. This function has two parameters.
The first parameter is a custom date string.
The second parameter specifies the format of the string
eg: Query the information of employees who were hired after noon on March 2, 1981
select ename ,hiredate from emp where hiredate > to_date('1981-03-02 12:00:00','YYYY-MM-DD HH24:MI: SS');
select T.ROLENAME e ,T.OPERATETIME from aes_user_role T where T.OPERATETIME > to_date('2017-07-02 12:00:00','YYYY-MM-DD HH24:MI:SS') order by T.Operatetime desc;
12. to_number
converts a string in a specific format into a number format. This function has two parameters.
The first parameter is a custom number string, and
the second parameter specifies the format of the string.
select ename,sal from emp where sal > to_number('$1,250.00','$9,999.99');//1250
select TO_NUMBER('199912'),TO_NUMBER('450.05') from dual;//199912,450.05
13. Group functions, Aggregation functions and grouping functions have the same meaning
avg: find the average
max: find the maximum value
min: find the minimum value
sum: sum
count: find the number of records
14. group by grouping
When you need to group data, use
eg: Calculate the average salary of each department
select mvg(sal) from emp group by deptno;
Rules: Fields that appear in the select list, if not in the group function, must appear in the group by clause.
Error eg: select ename ,max(sal) from emp group by deptno;
Error explanation: If grouped according to deptno, each group will have a maximum salary, but each group will have multiple names, which cannot be unique ename, so an error will be reported.
Normal eg:select t.enterprisedepid ,sysdate ,2*3 from enterprise_dep t group by t.
15. Use having to restrict grouping
If we need to exclude some specific groups from the grouping data, we need to use the Having keyword, for example: Take the average salary of the group whose average salary is greater than 1000 from the emp table
eg: select avg(sal) from emp group by deptno having avg(sal)>1000;
Comprehensive application:
we require a salary greater than 1200, grouped by department, and the average salary in the group must be greater than 1500 after these people are grouped, we need to query the grouping average salary.
select avg(sal) from emp where sal>1200 group by deptno having avg(sal)>1500 order by avg(sal);
Explanation: In the above statement, the Where statement will be executed first to filter out records that do not meet the selection conditions;
then Then group the filtered data according to the fields in the group by clause;
then use the having clause to filter out the groups that do not meet the conditions;
and then sort and display the remaining data.
Common sql summary
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=326184690&siteId=291194637
Recommended
Ranking