昨天我們安裝了"AdventureWorks範例資料庫",
這次想使用"AdventureWorks範例資料庫"來練習的主要原因,
是因為我覺得資料庫的資料量還算龐大,
且像我剛進公司一樣,
對每張資料表都陌生無比。
AdventureWorks範例資料庫中的資料表
和公司的資料表,
常常都帶給我那種「初次見面」、「一頭霧水」的傻眼感,
就像下面[HumanResources].[EmployeeDepartmentHistory]這張表,
滿滿的數字,
卻感覺沒有得到任何資訊,
(內心:我到底看了什麼?)
但從左邊的Table名似乎也不難猜,
於是我們來JOIN看看吧!
SELECT
E.BusinessEntityID 'E.BusinessEntityID'
,B.BusinessEntityID 'B.BusinessEntityID'
,E.DepartmentID 'E.DepartmentID'
,D.DepartmentID 'D.DepartmentID'
,E.ShiftID 'E.ShiftID'
,S.ShiftID 'S.ShiftID'
,*
FROM [HumanResources].[EmployeeDepartmentHistory] E
JOIN [Person].[BusinessEntity] B ON E.BusinessEntityID = B.BusinessEntityID
JOIN [HumanResources].[Department] D ON E.DepartmentID = D.DepartmentID
JOIN [HumanResources].[Shift] S ON E.ShiftID = S.ShiftID
這樣我們能夠得到的Table會變成這樣:
再把有意義的文字帶入,
SELECT
E.BusinessEntityID 'E.BusinessEntityID'
,B.BusinessEntityID 'B.BusinessEntityID'
,E.DepartmentID 'E.DepartmentID'
,D.Name 'D.DepartmentID'--有意義的欄位Name
,E.ShiftID 'E.ShiftID'
,S.Name 'S.ShiftID'--有意義的欄位Name
,*
FROM [HumanResources].[EmployeeDepartmentHistory] E
JOIN [Person].[BusinessEntity] B ON E.BusinessEntityID = B.BusinessEntityID
JOIN [HumanResources].[Department] D ON E.DepartmentID = D.DepartmentID
JOIN [HumanResources].[Shift] S ON E.ShiftID = S.ShiftID
會長這樣:
是不是有比較進入狀況一點了(吧?)
今天我們先看到這裡,
明天會繼續複習一年級的JOIN,
感謝閱讀,歡迎指教~