SQL标准语言内容大全(3)

2019-01-19 12:57

2、like使用的通配符:

% 代表0个或任意多个字符

_ (下划线)代表任意一个字符,一定会有一个,不能没有

3、通配符组合:通配符可以放各种位置,也可以互相组合,比如:

s% //s字符开头的字符串 %s //s字符结尾的字符串 _s% //第二个字符为s,后面随意 _s //总共两个字符,第二个为s

4、由于%和_用作了通配符,如果内容中本身有%或_的,就需要用到反义,如:

job_id like '%SA\\_%' escape '\\';

//escape '\\表示'用\\作为反义符,表示包含sa_的任意字符串

事实上,使用'\\'作为反义不需要加escape(它是默认的),如果想使用其它字符作为反义符,一定要加escape,例如

job_id like '%SA@_%' escape '@';

演示-like

搜索1995年进入公司的员工

select last_name,hire_date from hr.employees

where hire_date like '1999%';

练习-like通配符

(5分钟)搜索姓氏(last_name)中第三个字母为a的所有员工。 练习-销售岗位的员工

(5分钟)销售岗位的编码为SA_,请你搜索出所有销售岗位的员工信息:employee_id, last_name, job_id。

3-5 null条件

is null用来测试是否空值,is not nul表示非空,例如

select last_name, job_id,commission_pct from hr.employees

where commission_pct is null;

以上语句查询没有提成比例的员工,即非销售线员工。 练习-公司老大

(5分钟)查询公司所有员工的老大。

提示:所有员工都有经理,只有老大是没有经理的。

3-6逻辑条件

1、到目前为止,所有where条件都只有一个条件,有时候需要用到多个条件进行更精准的搜索,需要用到and 和 or 运算符。 2、逻辑运算符:

and 条件1 and 条件2 两个条件都满足才为true or 条件1 or 条件2 其中一个条件满足就为true not not 条件 取反

3、and的例子:

select employee_id, last_name, department_id, salary from hr.employees

where salary >= 10000 and department_id = 90;

4、or的例子:

select employee_id, last_name, department_id, salary from hr.employees

where salary >= 10000 or department_id = 90;

演示-逻辑运算符 演示and 和 or

select employee_id, last_name, department_id, salary from hr.employees

where salary >= 10000 and department_id = 90;

select employee_id, last_name, department_id, salary from hr.employees

where salary >= 10000 or department_id = 90;

练习-部门和薪资

(5分钟)查询工资在5000和12000之间并且在部门20或50中的员工的姓氏、部门、工资。提示:用between和in的组合 练习-like组合

(5分钟)查询出所有姓氏中有'a' 或者有 'e'的所有员工。

3-7优先顺序

1、where条件的优先顺序如下:

1、算术运算符 * / + - 2、连接运算符 ||

3、比较条件 = > >= < <= <> !=

4、is [not] null、[not] like、[not] in 5、[not] between 6、not 逻辑条件 7、and 逻辑条件

8、or 逻辑条件

2、如果写复杂条件组合的时候,为防止出现顺序错误,建议用小括号括起来。例如以下两个条件:

条件1:岗位是销售代表 或者 岗位是总裁并且工资超过15000

where job_id = 'SA_REP' or job_id = 'AD_PRES'

and salary > 15000;

条件2:岗位是销售代表或者总裁, 并且工资超过15000

where (job_id = 'SA_REP' or job_id = 'AD_PRES')

and salary > 15000;

练习-组合条件

(5分钟)查询出职位为销售代表(SA_REP)并且工资超过10000的,或者职位为仓库管理员(ST_CLERK)并且工资不等于2500、3500或7000的所有员工的姓氏、职务、工资。

3-8 order by结果排序

1、查询出结果以后,顺序是没有定义的,可以使用order by子句来进行排序,order by子句必须放在sql语句的最后。

order by {column, expr} [ASC | DESC];

2、order by必须要指定排序的对象,可以是列、表达式、别名。 3、排序方式有两种:asc升序和desc逆序,默认是升序。

4、排序以后,往往伴随着分页查询,例如:查询公司里工资最高的5个人

select last_name, salary from hr.employees order by salary desc

limit 5 offset 0;

//limit表示查询几条(一页几条),offset表示从哪里开始查

注:不同的数据库,分页机制有不同的实现,这里只是本系统数据库的实现方式,具体应用时要根据数据库类型而定。 演示-按聘用时间升序

演示order by:按聘用时间降序,时间从近到远,早的排在后面。

select last_name, hire_date from hr.employees

order by hire_date desc;

演示-按别名排序 按照年薪别名进行排序

select last_name, salary*12 annsal from hr.employees

order by annsal;

演示-多列排序

多列排序,先按部门降序,同一部门的按年薪升序

select last_name, department_id, salary*12 annsal from hr.employees

order by department_id desc, annsal;

练习-查询和排序

(5分钟)查询能赚取提成的员工的姓氏、工资和提成比率,按照工资和提成比率降序排列。 练习-字母排序

(5分钟)按照姓氏的字母顺序查询部门20和部门50中的所有员工的姓氏和部门编号。 练习-日期排序

(5分钟)查询在1998年2月20日和1998年5月1日之间聘用的员工的姓氏、职务、聘用日期,按照聘用日期进行升序排列。 练习-分页查询 (5分钟)

分页查询公司中按工资排序的员工姓氏和工资,假设一页为20条数据,现在要求查询出第3页的数据。

4-1等值关联

1、当需要查询多表关联数据时,可以在where使用联结条件。例如:

员工表里保存着部门编码,但是没有部门名称,部门名称在部门表里,现在需要查询出所有员工的姓氏、部门编码、部门名称:

select e.last_name, e.department_id, d.department_name from hr.employees e, hr.departments d

where e.department_id = d.department_id;

2、以上sql语句的where条件是一个等值联结,通过员工表中的部门编码 和部门表中的部门编码相等,来确定两个表的数据联结到一起。如果不加这一句联结条件,就会形成一个笛卡尔乘积(员工表的每一行 乘以 部门表的每一行)。例如:

select e.last_name, e.department_id, d.department_name from hr.employees e, hr.departments d;

3、等值联结语法:

select table1.column, table2.column from table1, table2

where table1.column1 = table2.column2;

找出所有两个表中这两个字段相等的记录,如果有一个表中此字段的值在另一表中不存在,则记录会被过滤。

也可以同时联结更多的表,一般n个表进行联结,需要n-1个联结条件。 演示-员工部门关联

查询所有员工的姓氏、部门编码、部门名称。

select e.last_name, e.department_id, d.department_name from hr.employees e, hr.departments d

where e.department_id = d.department_id;

注意:结果只有106条,观察缺少的那一条,department_id是null。

练习-员工和职务

(5分钟)查询所有员工的姓氏、职务编码、职务名称

4-2更多关联条件

1、在where里面可以增加更多的条件,从而更加精准的限制范围,例如:要查询员工Matos的部门编号和名称:

select e.last_name, e.department_id, d.department_name from hr.employees e, hr.departments d

where e.department_id = d.department_id and e.last_name = 'Matos';

2、表别名:

from hr.employees e 表示将employees表用别名e代替。

表别名使得语句精简一些,如果使用了表别名,那么整个语句中都必须使用这个表别名来代替表名。

3、用and可以联结更多的表,例如:

三表关联,显示所有员工的姓氏(员工表)、部门名称(部门表)、所在城市(场所表)

select e.last_name, d.department_name, l.city

from hr.employees e, hr.departments d, hr.locations l

where e.department_id = d.department_id and d.location_id = l.location_id;

演示-三表关联

查询所有员工的姓氏、部门名称、所在城市,需要用到三表关联,其中: employees表与departments表关联,departments表与locations表关联。 练习-赚提成员工的关联信息

(10分钟)查询所有能赚取提成的员工的姓氏、部门名称、办公城市。 提示:1、三表关联2、提成比例

4-3自联结

有时候,我们需要将表联结到自身,例如:查找员工Whalen的经理姓名。

select e.last_name, e.manager_id, m.last_name from hr.employees e, hr.employees m

where e.manager_id = m.employee_id and e.last_name = 'Whalen';

在这个例子里,employee表被模拟为两个表,e和m,联结条件为e的经理编号 = m的员工编码,最终查询出员工的经理姓名。 演示-自联结

查找员工 Whalen的姓氏、经理编号、经理姓名。

select e.last_name, e.manager_id, m.last_name from hr.employees e, hr.employees m

where e.manager_id = m.employee_id


SQL标准语言内容大全(3).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:新人教版一年级语文下册期末字词专项复习题[1]

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: