您的位置:首页 > 数据库

[innoVation]SQL语句基础2 2017.11.16

2017-11-16 11:16 423 查看
select * from 表名 where 列名 = ' ' 



select * from 表名 where 列名 is null



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

范围内的取值

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

between and 包括两个端点

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



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



UPPER()函数 

select UPPER(LOWER(ename)) from emp



LENGTH()函数 

select LENGTH(ename) , ename from emp 



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

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



GROUP BY 分组

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

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



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

select * from emp where sal >(select AVG(sal) from emp) 



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



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

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



LIMIT 

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

select * from emp LIMIT 0 ,10



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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  SQL语句基础