终于把牛老师的那个项目退了,以后有时间去学习自己喜欢的东西了。
我喜欢写点东西,也喜欢胡扯,哈哈。
算了,开始正题。scott表是Oracle官网的Demo现在学习一个新知识,学习他的Demo最好不过了。
select * from emp;
select empno ,ename from emp;
select ename,sal*12 as "年薪" from emp;
-- as 可以省略,
select ename as "姓名",sal*12 as "年薪" ,sal as"月薪" ,job from emp;
select 5 from emp;
/*
distinct[不允许重复的]
*/
select deptno from emp;-- 14行记录
select distinct deptno from emp;
select distinct comm from emp;--空也也是个唯一的数值,也可以过滤掉。只剩下唯一的一个。
select distinct comm,deptno from emp;
-- 这行是把他们这一组的组合进行过滤了。
select comm, distinct deptno from emp;
--这一组有错,是因为前面的com是14行,
--然而后面的过滤了之后是三行。明显出现错误了。
/*
between
的用法,在某个范围之间。
*/
--查找工资在1500和3000之间所有员工之间的信息
select * from emp
where sal>=1500 and sal<=3000;
select * from emp
where sal between 1500 and 3000;
--和上面的相同查询到的记录都是7个。
--查询相反的区间。
select * from emp
where sal <1500 or sal >3000
order by sal desc; --这里面的or和and的区别,否的否是并且
select * from emp
where sal not between 1500 and 3000
order by sal asc;
--h和上面的写法是一样的,但是这里面需要注意or和and的用法就KO了
/*
in的用法[属于若干个孤立的数值。]
*/
select * from emp
where sal in (1500,3000);
--这样查询只有1500和3000的工资的记录被查找了出来了,
--等价于下面的。
select * from emp
where sal =1500 or sal =3000;
--有in就有not in
select * from emp
where sal not in(1500,3000);
--下面和上面的是等价的。
select * from emp
where sal !=1500 and sal !=3000;
select * from emp
where sal <>1500 and sal <>3000;
--<>这个也是不等于。
--下面是top的用法。
select * from emp;
select top(2) * from emp;--把前面查询到的两行进行输出
select top(30) percent * from emp;--把30%输出,14行记录输出了5行,所以不够的话是往上面走的。
select top(10) * from emp
where sal between 1500and 3000
order by sal desc
select * from emp
where comm !=null;
select * from emp
where comm <>null;
-- 总结NULL不能参与!= <>运算
select * from emp
where comm =null;
-- 也不能参与=运算。
select * from emp
where comm is null;
select * from emp
where comm is not null;
--总结,null可以参与的运算有 is 还有is not
--任何类型的数据都可以为NULL;
create table t1 (name varchar(20) ,cnt int ,riqi datetime);
insert into t1 values(null,null,null);
select * from t1;
--所有的数据类都可以为NULL
--输出每个员工的姓名,年薪,(年薪包括奖金)
select ename as "姓名", sal*12+comm as "年薪" from emp;--错误哦哦。
-- null不能参与一个具体值进行运算,最终结果就是没有结果。
-- 本程序证明了NULL不能参与任何算术运算,否则还是没有结果的NULL
-- 百度了一下
select ename as"姓名",sal *12+isnull(comm,0) as"年薪" from emp;
-- 现在就是KO了, isNull函数可以解决这些问题、
--isNull 如果comm是NILL就返回0 否则就返回comm
/*
下面是order by[以某个字段进行排序]
*/
select * from emp order by sal;
-- 这个默认是升序
select * from emp order by sal desc;
--这样子写是降序
select * from emp order by deptno ,sal ;
--这样子是先按deptno 排序,然后发现deptno相同的时候在按照sal排序。
-- 模糊查询。
select * from emp where ename like '%a%';--把含有A的字母的名字给查询出来
select * from emp where ename like 'a%'-- 首字母是A的咱们就输出
select * from emp where ename like '%a'-- 只要是末尾的字母是A的就打印
select * from emp where ename like '_a%' -- 只要第二个字母是A就打印
select * from emp where ename like '_[a-f]%'--只要是第二个字符是A-F的人打印
select * from emp where ename like '_[^a-f]%' -- 把上面的取反得到的
-- 加入我这里插入了这么一句,那么该如何查询到%的姓名呢,百度了一下得到了下面的结果
insert into emp values (0001,'c%d',NULL,0001,null,0,null,20);
select * from emp where ename like '%\%%' escape '\'
--看,这样就查询出来了答案了,关键是把\当成了特殊字符对待,后面的是一个普通字符。
-- 通配符
/*
和regex正则表达式一模一样 学过正则这些就不用再看了
-- attintion 唯一需要注意的就是把like后面的加上单引号,代表区分关键字,
说到了单引号,那么双引号在我们这里的使用代表的是一个对象的名称或者是别名。
% 任意0个或者多个字符
_ 下划线,代表的是任意的单个字符
[a-f] 代表的是a-f的任意的单个字符
[a,z] 代表的是a或者z
[^a-f]代表的是取反,不是a-z的任意字符
*/
/*
模糊查询完成。大概也就这么多东西了。2016年2月11日16:37:21
*/
/*
聚合函数
单行函数,每一行返回一个数值。
多行函数,多行返回一个数值.
聚合函数是多行的函数。
*/
select lower(ename) from emp; --大写换成lower写。单行函数,每一行都返回一个数值
select max(sal) from emp; --max是多行函数,因为多行返回的是一个数值。
/*
聚合函数的分类
max()
min()
avg()
count()
*/
select count(*) from emp --返回emp表里面所有记录的个数
select count(deptno) from emp;-- 有重复的but还是15这说明deptno重复的记录还是被当成有效的了
select count(distinct deptno) from emp; --挂上distinct 这样重复的记录就被过滤了。
select count(comm) from emp; -- null不参与count的计算。返回值是4个,然而不是14个
--------------------------------------------------------
select max(sal) as "最高工资" ,min(sal) as "最低工资" ,count(*) "员工人数" from emp; -- 正确
select max(sal) ,lower(ename) from emp;--返回的行数不一样,肯定是ERROR
--