SERVER-----查询(1)

目录

Scott库下载:

 一. 查询

1. 计算列

2. distinct

3. betwee... and...

4. in


Scott库下载:

        在大家学习数据库后期需要使用Scott库进行辅助学习,下面是我从一个叫做

yuhan_Li的博主那复制过来的,大家尽可能访问原文章吧,下面的链接是Scott的下载和附加教程。

https://www.cnblogs.com/fenglinyu/p/11067097.html

        附加之前,基本都要更改Scott文件的属性:右击属性——安全——完全控制

 一. 查询

        查询最重要,最常用,难度也最大,面试必考。

1. 计算列

--查询语句之间使用分号
select * from emp;--查询整张表

select ename from emp;--查询表中的一列

select ename,sal from emp;--查询表中多列,列名之间使用逗号隔开

select ename,sal*12 as "年薪" from emp;--更改列中数据以及字段名
--而且 as 可以省略,年薪单引号/没有引号 也行,只是移植到其他库里就不允许了,可移植性大大降低;

select ename,sal*12 "年薪", sal "月薪",job from emp;--更改多列数据重命名并输出字段名
--此外,我们需要了解逻辑上的执行。
--以 select ename,sal from emp 为例,我们先找到表emp,然后找到每条记录的 ename,sal,最后逐行输出;
 

         特殊:select 5 from emp;--如果是常量值,就会输出常量,且共有14行,因为 表emp 共有14行;

        下面是我们的使用代码;

                直接查询整张表和其中的一列也是没有问题的;

2. distinct

        distinct 的中文意思是:独特的,有明显区别的;因此其可用来过滤重复数据;

        以下是我们将使用的代码; 

select * from emp;--查询整张表;


select distinct deptno from emp;--过滤掉deptno的重复数据;


select distinct comm from emp;--重复的NULL也会被过滤;


select distinct deptno,comm from emp;--过滤掉组合重复的数据;


--select ename, distinct deptno,comm from emp;--报错,逻辑上错误,输出行数不等;

3. betwee... and...

        和 distinct 一样,因为是一种过滤数据的查询,下面是代码演示:

--Between(也是一种过滤)
select * from emp where 3<1;--3小于1为假,所以只输出字段;


select *from emp where 3>2;--3大于2为真,所以该限制无效,输出整张表;


select * from emp where sal>=2000 and sal<=3000;--等价于


select *from emp where sal between 2000 and 3000;--between...and...


select ename from emp where sal between 2000 and 3000;--多列查询


select *from emp where sal not between 2000 and 3000;--多列查询的not用法

4. in

        in 后面括号里跟的是某个字段的特定条件;下面是代码演示:

--in(后面跟某个特定的值)
select * from emp where sal in (1600,5000);--找表中sal为 1600 和 5000 的数据;
--等价于
select *from emp where sal=1600 or sal=5000; 

--也可以排除他俩进行输出
select * from emp where sal<>1600 and sal<>5000; --不等于有两种表示。!= ; < > ;推荐使用第二种
--等价于
select * from emp where sal not in (1600,5000);--not的用法

  • 45
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值