First, the operator:
1, Category:
Arithmetic, comparison, logical, set, is connected;
2, arithmetic operators:
Performing a numerical calculation;
- salary plus 1000
SELECT EMPNO, ename, Job, 1000 + SAL from EMP;
3, comparison operators:
- comparison operator (between and no header trailer)
SELECT * from EMP WHERE SAL BETWEEN 1000 and 2000;
(Not mean! =, Is in mysql <>)
4, the logical operators: and or not
- Query bonus is not empty employee information
select * from emp where comm is not null;
5, set operator:
The results of the two queries are combined into one result:
select sid from t_score where score>=70 and cid = '01'
INTERSECT
select sid from t_score where score>=70 and cid = '07';
①, intersect two rows back to the public inquiry;
②, union: Returns all rows of two duplicate query; there may be data in a table (another join queries in mysql - not a table)
③, minus: the second query returns exclude rows that appear in the results from the first query; (look at the first results that do not meet the second one)
6, connected to the operator:
The string or a plurality of data values into a single string;
- concatenation operator
select ( 'employee number' || empno || ', the name of' || ename || 'wages are' || sal) from emp
7. Priority:
Arithmetic> connected> Comparative> not Logic> and Logic> or Logical Operators
Two, SQL functions:
Function used to perform specific operations;
1, Category:
One-way, the packet analysis;
2, one-way function of classification:
Each row of the query from the table only returns a value;
Characters, numbers, date, conversion, other;
3, character function:
- String Functions
select lower (ename), job from emp; - lowercase
select ltrim ( 'xysdezadmas', 'xyz') from dual; - taken: Press the Start field starts behind taken from the left, there are several several interception, no not taken
①, other characters function:
chr: returns the corresponding character according to the ASCII code (0:48 a: 97 A: 65)
lpad and rpad: filling;
trim: removing the left and right sides of spaces;
lenth: string length;
decode: one by the replacement value;
select ename, decode (job, 'CLERK', 'staff', 'SALESMAN', 'shopping guide') as work from emp
4, the digital function;
5, the date function:
Calculates date values, and generates date data type or the result of numeric type;
add_months offset;
months_between: number of months difference;
last_day last day;
round: rounding;
trunc: are rounded Returns the date mode truncated after the first day;
next_day (d, day): Date of the week next week
extract: calculated difference in years:
- Date function
select add_months (sbirth, -3) from java0322; - month offset in March, can be negative, which is offset forward
select extract (year from sysdate) from dual; - current time of year, taken
select sname,extract(year from sysdate) - extract(year from sbirth) from java0322;--计算年纪
6、转换函数:
to_char:按照指定的格式转化字符串;
to_date:将字符串转化为日期;
to_number:将数字字符串转化为数字;----可以实现直接在sql语句中进行格式的转换
--转换函数(字符串,格式)
Select TO_CHAR(0.123,'$0.9999') FROM DUAL;
--字符串转日期
select to_date ('2005-02-28','yyyy-mm-dd') from dual;
--字符串转数字
select to_number('20') from dual;
其他函数:
nvl:isnull,不为空时为本身,为空时指定0;
nvl2:不为空时指定值2,为空时指定值3;
NullIF:相等时为空,否则为前者
--查询员工表的所有人的姓名,工资+奖金(奖金没有的时候,由null转为0)---nvl转换空值的函数
select ename,sal+nvl(comm,0)as sal from emp;--当空为0,不为空为comm
select ename,sal+nvl2(comm,10000,0) from emp;---不为空时10000,为空时0;
select nullif(100,200) from dual;--相等为空,不等为前者
7、分组函数:
基于一组行来返回结果;
avg、min、max、sum、count
8、分析函数:
根据一组行来计算聚合值;用于计算完成聚集的累计排名、移动平均数等;
row_number:返回连续的排位,不论值是否相等;
rank:具有相等值的行排位相同,序数随后跳跃;
dense_rank:具有相等值的行排位相同,序号是连续的
-- 排位
select empno,ename,job,sal,row_number()over (order by sal desc) as numm from emp; --返回连续的排位,不论值是否相等12345
select empno,ename,job,sal,rank()over (order by sal desc) as numm from emp; --相等值的行排位相同,序号随后跳跃;12245
select empno,ename,job,sal,dense_rank()over (order by sal desc) as numm from emp; --相等值的行排位相同,序号是连续的;12234