SQL Server 2008データベースに複数のテーブルがあり、それらを結合してからクエリする必要がある このデータベースには以下のテーブルがあります。
**Address** with unique reference column AddressNo
**Employee** with unique reference columns AddressNo & PersonNo
**EmploymentLink** with unique reference columns PersonNo & BusinessNo
**Business** with unique reference column BusinessNo
**BusinessSICCode** with unique reference column BusinessNo
複数のテーブルがあるため、すべてのテーブルにまたがってデータを取得するためにクエリを実行できるように、これらすべてのテーブルをリンクしたいと思います。
以下は私がこれまでに思いついた質問です。
SELECT Employee.*, Address.*,Business.*,BusinessSICCode.*
FROM Employee,Address,Business,BusinessSICCode
JOIN Employee ON Employee.AddressNo = Address.AddressNo
JOIN Employee ON Employee.PersonNo = EmploymentLink.PersonNo
JOIN Business ON Business.BusinessNo = EmploymentLink.BusinessNo
JOIN BusinessSICCode ON BusinessSICCode.BusinessNo = Business.BusinessNo
回答:
回答№1は0このような何かがあなたを軌道に乗せるはずです
SELECT * FROM
Address a INNER JOIN Employee e ON e.AdressNo = a.AddressNo
INNER JOIN EmploymentLine l on l.PersonNo = e.PersonNo
INNER JOIN Business b ON b.BusinessNo = l.BusinessNo
INNER JOIN BusinessSICCode c ON c.BusinessNo = b.BusinessNo
レコードが含まれていなくても親テーブルの値が必要なテーブルがいくつかある場合は、INNER JOINSをLEFT JOINSに変更できます。