2016-04-03 76 views
2

你是我最後的希望。我花了整整一天,但我還沒有決定如何創建這個查詢。複雜的內部連接sql

這是我目前數據庫圖表

enter image description here

每一個存儲有1個名冊

每名冊中的首席和工人組成。關於他們的信息在東西桌上。

我想要做什麼?獲取所有存儲(方形地址),worker_name,worker_surname,chief_name,chief_surname。

我至今

select storage_address, 
storage_square, 
stuffs.stuff_name as chiefSurname, 
stuffs.stuff_surname as chiefName from storages 
inner join storageRoster on storageRoster.storageRoster_id=storages.storage_roster_id 
inner join Chiefs on storageRoster.chief_id = Chiefs.chief_id 
inner join stuffs on Chiefs.chief_stuff_id = stuffs.stuff_id 

但在此查詢我只能得到每一個存儲首領。請幫助。我很絕望。

+0

內部聯接將返回只匹配,與出的數據,其硬牛逼的幫助下,你可以通過保持一個嘗試調試一次加入 – TheGameiswar

+0

您必須將工人和其他材料(用於獲取工人數據)加入您的查詢。 – RubioRic

回答

3

你需要加入的東西表兩次,一次爲工人和一次首席:

select storage_address, 
storage_square, 
cs.stuff_name as chiefSurname, 
cs.stuff_surname as chiefName, 
ws.stuff_name as workerSurname, 
ws.stuff_surname as workerName 
from storages 
inner join storageRoster on storageRoster.storageRoster_id=storages.storage_roster_id 
inner join Chiefs on storageRoster.chief_id = Chiefs.chief_id 
inner join stuffs cs on Chiefs.chief_stuff_id = cs.stuff_id 
inner join Workers on storageRoster.worker_id = Workers.chief_id 
inner join ws cs on Workers.worker_stuff_id = ws.stuff_id 
+0

很高興幫助:-) –