2012-09-24 128 views
0

好吧,我有我的數據庫表5個,有如下幾點 officelocations_tbl state_tbl city_tbl staff_tbl titles_tbl獲取所有表中的數據

titles表只與工作人員表關聯,但其他人所有的內部加入。我已經嘗試過各種mysql語句,但沒有人允許我帶來titles_tbl。 這裏是sql語句我試圖使用最新版本:

SELECT officelocations_tbl.*,city_tbl.*, state_tbl.* , titles_tbl.*, 
    contact1.firstName AS c1Firstname, contact1.lastName AS c1lastName, 
    contact1.middleInitial AS c1middleInitial, contact1.suffix AS c1suffix, 
    contact1.accredations AS c1accredations, contact1.phone AS c1Phone, 
    contact1.faxNumber AS c1FaxNumber, contact1.mobilePhone AS c1Mobile, 
    contact1.email AS c1Email, contact1.titleID AS c1Title, 

    contact2.firstName AS c2Firstname, contact2.lastName AS c2lastName, 
    contact2.middleInitial AS c2middleInitial, contact2.suffix AS c2suffix, 
    contact2.accredations AS c2accredations, contact2.phone AS c2Phone, 
    contact2.faxNumber AS c2FaxNumber, contact2.mobilePhone AS c2Mobile, 
    contact2.email AS c2Email, contact2.titleID AS c2Title, 

    partner.firstName AS c3Firstname, partner.lastName AS c3lastName, 
    partner.middleInitial AS c3middleInitial, partner.suffix AS c3suffix, 
    partner.accredations AS c3accredations, partner.phone AS c3Phone, 
    partner.faxNumber AS c3FaxNumber, partner.mobilePhone AS c3Mobile, 
    partner.email AS c3Email, partner.titleID AS c3Title 

    FROM officelocations_tbl 

    JOIN city_tbl ON (officelocations_tbl.cityID = city_tbl.cityID) 
    INNER JOIN titles_tbl ON titles_tbl.titleID = staff_tbl.titleID 
    LEFT OUTER JOIN state_tbl ON (officelocations_tbl.stateID = state_tbl.stateID) 
    LEFT OUTER JOIN staff_tbl contact1 ON (contact1.staffID = officelocations_tbl.contact1) 
    LEFT OUTER JOIN staff_tbl contact2 ON (contact2.staffID = officelocations_tbl.contact2) 
    LEFT OUTER JOIN staff_tbl partner ON (partner.staffID = officelocations_tbl.partner) 

但是這給了我一個錯誤[ERR] 1054 - 未知列「staff_tbl.titleID」在「關於條款」。如果我刪除兩條線:

INNER JOIN titles_tbl ON titles_tbl.titleID = staff_tbl.titleID 
    titles_tbl.*, 

它的作品,但不拉標題。我嘗試過這樣做,但是它只吸引了一次標題,而不是全部三個聯繫人。

SELECT 
    staff_tbl.staffID, 
    staff_tbl.staffID_C2, 
    staff_tbl.staffID_P, 
    staff_tbl.firstName, 
    staff_tbl.middleInitial, 
    staff_tbl.lastName, 
    staff_tbl.suffix, 
    staff_tbl.accredations, 
    staff_tbl.email, 
    staff_tbl.phone, 
    staff_tbl.mobilePhone, 
    staff_tbl.officePhone, 
    staff_tbl.faxNumber, 
    staff_tbl.address1, 
    staff_tbl.address2, 
    staff_tbl.cityID, 
    staff_tbl.stateID, 
    staff_tbl.zipCode, 
    staff_tbl.titleID, 
    staff_tbl.locationID, 
    staff_tbl.photoURL, 
    staff_tbl.vCardURL, 
    staff_tbl.qRCodeURL, 
    staff_tbl.resumeURL, 
    staff_tbl.biography, 
    staff_tbl.dateCreated, 
    officelocations_tbl.locationID, 
    officelocations_tbl.officeName, 
    officelocations_tbl.address1, 
    officelocations_tbl.address2, 
    officelocations_tbl.cityID, 
    officelocations_tbl.stateID, 
    officelocations_tbl.zipCode, 
    officelocations_tbl.officePhone, 
    officelocations_tbl.contact1, 
    officelocations_tbl.contact2, 
    officelocations_tbl.partner, 
    city_tbl.cityID, 
    city_tbl.cityName, 
    state_tbl.stateID, 
    state_tbl.state_abreviation, 
    state_tbl.state_name, 
    titles_tbl.titleID, 
    titles_tbl.titleName, 

    contact1.firstName AS c1Firstname, contact1.lastName AS c1lastName, 
    contact1.middleInitial AS c1middleInitial, contact1.suffix AS c1suffix, 
    contact1.accredations AS c1accredations, contact1.phone AS c1Phone, 
    contact1.faxNumber AS c1FaxNumber, contact1.mobilePhone AS c1Mobile, 
    contact1.email AS c1Email, contact1.titleID AS c1Title, 

    contact2.firstName AS c2Firstname, contact2.lastName AS c2lastName, 
    contact2.middleInitial AS c2middleInitial, contact2.suffix AS c2suffix, 
    contact2.accredations AS c2accredations, contact2.phone AS c2Phone, 
    contact2.faxNumber AS c2FaxNumber, contact2.mobilePhone AS c2Mobile, 
    contact2.email AS c2Email, contact2.titleID AS c2Title, 

    partner.firstName AS c3Firstname, partner.lastName AS c3lastName, 
    partner.middleInitial AS c3middleInitial, partner.suffix AS c3suffix, 
    partner.accredations AS c3accredations, partner.phone AS c3Phone, 
    partner.faxNumber AS c3FaxNumber, partner.mobilePhone AS c3Mobile, 
    partner.email AS c3Email, partner.titleID AS c3Title 

    FROM officelocations_tbl 
    INNER JOIN staff_tbl ON staff_tbl.staffID = officelocations_tbl.contact1 
    INNER JOIN state_tbl ON state_tbl.stateID = officelocations_tbl.stateID 
    INNER JOIN titles_tbl ON titles_tbl.titleID = staff_tbl.titleID 
    INNER JOIN city_tbl ON city_tbl.cityID = officelocations_tbl.cityID 

    LEFT OUTER JOIN staff_tbl contact1 ON (contact1.staffID = officelocations_tbl.contact1) 
    LEFT OUTER JOIN staff_tbl contact2 ON (contact2.staffID = officelocations_tbl.contact2) 
    LEFT OUTER JOIN staff_tbl partner ON (partner.staffID = officelocations_tbl.partner) 

這將只用於拉= staff_tbl.staffID的officelocations_tbl.contact1第一關聯。我難以接受下一步要做什麼。有沒有人會知道如何讓它拉動所有5張桌子?

+0

這可能是一個愚蠢的問題,但你確定staff_tbl包含列titleID嗎? –

+0

是的,它確實存在 – puddleJumper

回答

0

您只需將INNER JOIN向下移動即可,例如,從

INNER JOIN titles_tbl ON titles_tbl.titleID = staff_tbl.titleID 
LEFT OUTER JOIN state_tbl ON (officelocations_tbl.stateID = state_tbl.stateID) 

LEFT OUTER JOIN state_tbl ON (officelocations_tbl.stateID = state_tbl.stateID) 
INNER JOIN titles_tbl ON titles_tbl.titleID = staff_tbl.titleID 

在語法分析器到達INNER JOIN的時候,staff_tbl尚未加入,解析器不會「向前看」,看看它的後期加入。所以它立即保留一個「沒有這樣的表/字段」。

切換髮生這種情況的順序允許首先加入taff_table,然後您可以在更多連接中使用它。

+0

仍然像以前一樣得到同樣的錯誤信息[Err] 1054 - 'on子句'中的未知列'staff_tbl.titleID'。如果我刪除了內部聯接以及選擇titles表中的所有信息,它將起作用,但不會僅將標題拉到標題id上。 – puddleJumper

+0

糟糕,在這部分應該有另一個左連接。一秒來編輯答案......並且只是注意到你將別名的staff_tbl別名,不會留下實際的「staff_tbl」。 staff_tbl上的另一個加入* NO *別名會有所幫助。 –

+0

不知道我能做到這一點。我嘗試過一次,但問題是我只在staff_ID的職員表中有一個位置,在contacts_1,contact2和合夥人位置中有三個位置在locations_tbl之一。當我嘗試將1個staff_ID鏈接到locations_tbl中的3個不同點時,它會出錯。 – puddleJumper

相關問題