Showing posts with label SQL select two tables which have relationship with one another table. Show all posts
Showing posts with label SQL select two tables which have relationship with one another table. Show all posts

Thursday, June 28, 2012

គន្លឹះ SQL tweek

ឧបមាថាខ្ញុំមាន​ Table ដូចខាងក្រោម៖

Players (Columns: Id, PlayerName, PlayerNumber, LocationId)
Partners (Columns: Id, PartnerName, PartnerNumber, LocationId)
Locations (Columns: Id, Location)
Registration(Columns:​​ Id, PlayerId, PartnerId, RegDate)

ខ្ញុំចង់ Select PlayerName , PlayerNumber, Location, PartnerName, PartnerNumber, Location ពី Table ខាងលើតើខ្ញុំត្រូវ បង្កើត sql query ដោយរបៀបណា? 

ចំលើយគឺ

SELECT  PlayerName , PlayerNumber, L1.Location, PartnerName, PartnerNumber, L2.Location 
FROM Registration 
INNER JOIN Players ON Registration.PlayerId = Players.Id 
INNER JOIN Partners ON Registration.PartnerId = Partners.Id 
INNER JOIN Locations L1 ON Players.LocationId = L1.Id 
INNER JOIN Locations L2 ON Partners.Id = L2.Id