Home > Database > Mysql Tutorial > body text

SQL 巩固练习(用到了前几天几个知识点)

WBOY
Release: 2016-06-07 17:44:08
Original
913 people have browsed it

SQL 巩固练习用到了前几天几个知识点 在查看SQL Server 2005的帮助文档中的cross apply 运算符时发现了一个个人感觉用到的知识点儿比较多,比较经典的例子。在此写下来,也是为了巩固一下前几天的知识点。 先建表一员工表(Employees): CREATE TABLE Emplo

SQL 巩固练习——用到了前几天几个知识点

  在查看SQL Server 2005的帮助文档中的cross apply 运算符时发现了一个个人感觉用到的知识点儿比较多,免备案空间,比较经典的例子。在此写下来,香港空间,也是为了巩固一下前几天的知识点。

先建表一员工表(Employees):

CREATE TABLE Employees ( empid , mgrid int NULL, empname varchar(25) NOT NULL, salary , CONSTRAINT PK_Employees PRIMARY KEY(empid), )

向Employees表中插入数据:

Employees , $10000.00) , $5000.00) , $5000.00) , $5000.00) , $2500.00) , $2500.00) , $2500.00) , $2500.00) , $2500.00) , $2500.00) , $2000.00) , $2000.00) , $2000.00) , $1500.00)

查询向Employees表插入的全部数据SQL语句:

Employees

结果如图:

再建表二部门表(Departments):

CREATE TABLE Departments ( deptid , deptname VARCHAR(25) NOT NULL, deptmgrid Employees )

Departments表插入数据:

, 2) , 7) , 8) , 9) , 4) , NULL)

查询向Departments表插入的全部数据SQL语句:

Departments

结果如图:

下面的表值函数使用雇员 ID 作为参数,并返回该雇员及他/她的所有下属(用到了前面所学的CTE公共表表达式 with...as... 的递归):

) ( empid , empname VARCHAR(25) NOT NULL, mgrid INT NULL, lvl ) Employees_Subtree(empid, empname, mgrid, lvl) AS ( empid, empname, mgrid, 0 FROM employees e.empid, e.empname, e.mgrid, es.lvlemployees AS e JOIN employees_subtree AS es ON e.mgrid = es.empid ) Employees_Subtree

返回每个部门经理的所有级别的全部下属,使用下面的SQL语句(用到了apply的cross apply):

Departments AS D CROSS APPLY fn_getsubtree(D.deptmgrid) AS ST

结果如图:

,虚拟主机
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!