Saturday, February 14, 2015
If you look at the Employees table in the Northwind database diagram you will see that there's a relationship that links to itself
And if you look the at the Employees create script you will see that the foreign key to is the ReportTo field referencing the Primary Key EmployeeID. This kind of self referencing is called a unary relationship.
So how do you query the employees who is manage by another employee? You can assign aliases to the same table so that you can query the same table as if it were two different tables.
Here are the results:
As you can see from the select list, (e.FirstName + ' ' + e.LastName) AS Name displays the Employee's name, but (et.FirstName + ' ' + et.LastName) AS Supervisor displays the supervisor's name even though we are retrieving the same fields on the same table. This behavior is possible because we gave the same table two different aliases. As a general rule, you should replace subqueries with self joins, if you can because it performs better.
And if you look the at the Employees create script you will see that the foreign key to is the ReportTo field referencing the Primary Key EmployeeID. This kind of self referencing is called a unary relationship.
ALTER TABLE [dbo].[Employees] WITH NOCHECK ADD CONSTRAINT [FK_Employees_Employees] FOREIGN KEY([ReportsTo]) REFERENCES [dbo].[Employees] ([EmployeeID])
So how do you query the employees who is manage by another employee? You can assign aliases to the same table so that you can query the same table as if it were two different tables.
SELECT e.EmployeeID,(e.FirstName + ' ' + e.LastName) AS Name, (et.FirstName + ' ' + et.LastName) AS Supervisor FROM Employees e, Employees et WHERE e.ReportsTo = et.EmployeeID
Here are the results:
As you can see from the select list, (e.FirstName + ' ' + e.LastName) AS Name displays the Employee's name, but (et.FirstName + ' ' + et.LastName) AS Supervisor displays the supervisor's name even though we are retrieving the same fields on the same table. This behavior is possible because we gave the same table two different aliases. As a general rule, you should replace subqueries with self joins, if you can because it performs better.
Subscribe to:
Post Comments (Atom)
Search This Blog
Tags
Web Development
Linux
Javascript
DATA
CentOS
ASPNET
SQL Server
Cloud Computing
ASP.NET Core
ASP.NET MVC
SQL
Virtualization
AWS
Database
ADO.NET
AngularJS
C#
CSS
EC2
Iaas
System Administrator
Azure
Computer Programming
JQuery
Coding
ASP.NET MVC 5
Entity Framework Core
Web Design
Infrastructure
Networking
Visual Studio
Errors
T-SQL
Ubuntu
Stored Procedures
ACME Bank
Bootstrap
Computer Networking
Entity Framework
Load Balancer
MongoDB
NoSQL
Node.js
Oracle
VirtualBox
Container
Docker
Fedora
Java
Source Control
git
ExpressJS
MySQL
NuGet
Blogger
Blogging
Bower.js
Data Science
JSON
JavaEE
Web Api
DBMS
DevOps
HTML5
MVC
SPA
Storage
github
AJAX
Big Data
Design Pattern
Eclipse IDE
Elastic IP
GIMP
Graphics Design
Heroku
Linux Mint
Postman
R
SSL
Security
Visual Studio Code
ASP.NET MVC 4
CLI
Linux Commands
Powershell
Python
Server
Software Development
Subnets
Telerik
VPC
Windows Server 2016
angular-seed
font-awesome
log4net
servlets
tomcat
AWS CloudWatch
Active Directory
Angular
Blockchain
Collections
Compatibility
Cryptocurrency
DIgital Life
DNS
Downloads
Google Blogger
Google Chrome
Google Fonts
Hadoop
IAM
KnockoutJS
LINQ
Linux Performance
Logging
Mobile-First
Open Source
Prototype
R Programming
Responsive
Route 53
S3
SELinux
Software
Unix
View
Web Forms
WildFly
XML
cshtml
githu
Aivivu đại lý vé máy bay, tham khảo
ReplyDeletevé máy bay đi Mỹ bao nhiêu tiền
các chuyến bay từ mỹ về việt nam hôm nay
vé máy bay từ canada về việt nam giá rẻ
vé máy bay từ nhật về việt nam bao nhiêu tiền
Máy bay từ Hàn Quốc về Việt Nam
Vé máy bay từ Đài Loan về Việt Nam
ve may bay chuyen gia nuoc ngoai sang Viet Nam