[innoVation]SQL语句基础2 2017.11.16

select * from 表名 where 列名 = ' ' 

[innoVation]SQL语句基础2 2017.11.16

select * from 表名 where 列名 is null

[innoVation]SQL语句基础2 2017.11.16

注意null 为 列名 is null , ' ' 为 列名 = ' '。



范围内的取值

select * from 表名 where 列名 >= 1 and 列名 <= 9


between and 包括两个端点

select * from 表名 where 列名 BETWEEN 7369 and 7566

[innoVation]SQL语句基础2 2017.11.16


order by  排序

order by语句用于对结果集进行排序

order by 语句用于根据指定列对结果集进行排序

order by 语句默认按照升序对记录进行排序

升序asc 降序为 desc

select empno from emp ORDER BY empno DESC 降序

select empno from emp ORDER BY empno ASC 升序


LOWER()函数 

select LOWER(ename)小写名称,ename from emp

[innoVation]SQL语句基础2 2017.11.16

UPPER()函数 

select UPPER(LOWER(ename)) from emp

[innoVation]SQL语句基础2 2017.11.16

LENGTH()函数 

select LENGTH(ename) , ename from emp 

[innoVation]SQL语句基础2 2017.11.16

COUNT()函数  AVG()函数 MAX()函数 MIN()函数 SUM()函数

select count(*) 员工人数, avg(sal) 平均工资, MAX(sal)最高工资 , MIN(sal)最低工资 ,SUM(sal) 总合 from emp

[innoVation]SQL语句基础2 2017.11.16


GROUP BY 分组

注意:分组后过滤条件要写在HAVING后

select deptno,COUNT(*) from emp GROUP BY deptno HAVING deptno > 10

[innoVation]SQL语句基础2 2017.11.16

所有主函数不能直接使用,只能查询一次后才能使用。

select * from emp where sal >(select AVG(sal) from emp) 
[innoVation]SQL语句基础2 2017.11.16


SELECT * from emp where empno in(select empno from emp where empno > 7500)


联表查询

用逗号联表查询后,and后跟筛选条件

elect e.*,d.dname from emp e, dept d where e.deptno = d.deptno and sal > 1000

[innoVation]SQL语句基础2 2017.11.16

联表条件写在ON后,where后跟筛选条件

select e.*,d.dname from emp e INNER JOIN dept d ON e.deptno = d.deptno where e.sal > 1000

[innoVation]SQL语句基础2 2017.11.16


LIMIT 

可以强制select语句返回指定的记录数。

select * from emp LIMIT 0 ,10

[innoVation]SQL语句基础2 2017.11.16



select * from user11 where userId in (2,3,4) 指定看那几条记录。


[innoVation]SQL语句基础2 2017.11.16