2013-12-23 34 views
1

我有三個表。這是在住房行業。所以我有一個單位表,一個便利表和一個用戶定義表。對角線結果的一對多SQL查詢

對於單位表中的每個條目,我想在一行中顯示所有設施和用戶定義的值。

UnitID | UnitName 
---------|--------------- 
    1  | 123 Main St 
    2  | 456 Fake Rd 
    3  | 789 Madeup Blvd 

設施表

UnitID | Amenity Desc 
---------|----------------- 
    1  | Has Pool 
    1  | Has Parking 
    1  | Includes Electric 
    2  | Has Parking 
    2  | Large Closets 

用戶自定義表

UnitID | UDF Desc 
---------|------------------ 
    1  | Property MGR 
    1  | Show Online 
    2  | Property MGR 
    2  | Don't Show Online 

我需要顯示的

UnitID | Name   | Pool | Park  | UDF1 | UDF2 ...And so on 
---------|------------------|-----------|------------|------------|------------- 
    1  | 123 Main St  | Has Pool | Incl Prkg | Prp MGR | ShowOnline 
    2  | 456 Fake Rd  |   | Incl Prkg | Prp MGR | Don't Online 

的問題是,當我運行我的查詢涉及像這樣:

UnitID | Name   | Pool | Park  | UDF1 | UDF2 ...And so on 
---------|------------------|-----------|------------|------------|------------- 
    1  | 123 Main St  |   |   |   | ShowOnline 
    1  | 123 Main St  |   |   | Prp MGR | 
    1  | 123 Main St  |   | Incl Prkg |   | 
    1  | 123 Main St  | Has Pool |   |   | 
    2  | 456 Fake Rd  |   |   |   | Don't Online 
    2  | 456 Fake Rd  |   |   | Prp MGR | 
    2  | 456 Fake Rd  |   | Incl Prkg |   | 

我確定以前人們都看到過這個,所以我很好奇這樣做需要什麼。我錯過了什麼?

下面是查詢,因爲我現在有,如果有幫助: 僅供參考,我也試着用DISTINCT參數運行它。

SELECT units.propid, 
     units.name, 
     units.street1, 
     units.city, 
     units.state, 
     units.zip, 
     units.sqft, 
     units.bedrooms, 
     units.bathrooms, 
     unittypes.unittype, 
     "" 
     AS AMENITIESS, 
     Iif([unitamenities].[amenityid] = 1, true, false) 
     AS Fireplace, 
     Iif([unitamenities].[amenityid] = 2, true, false) 
     AS Carpet, 
     Iif([unitamenities].[amenityid] = 3, true, false) 
     AS Hardwood, 
     Iif([unitamenities].[amenityid] = 4, true, false) 
     AS Dishwasher, 
     Iif([unitamenities].[amenityid] = 5, true, false) 
     AS Furnished, 
     Iif([unitamenities].[amenityid] = 6, true, false) 
     AS [Electricity Included], 
     Iif([unitamenities].[amenityid] = 7, true, false) 
     AS [Trash Included], 
     Iif([unitamenities].[amenityid] = 8, true, false) 
     AS [Has A/C], 
     Iif([unitamenities].[amenityid] = 9, true, false) 
     AS [Deck;Patio; or Porch], 
     Iif([unitamenities].[amenityid] = 10, true, false) 
     AS [Laundry in Unit], 
     Iif([unitamenities].[amenityid] = 3, true, false) 
     AS [Laundry on site], 
     Iif([unitamenities].[amenityid] = 12, true, false) 
     AS [Has Storage], 
     Iif([unitamenities].[amenityid] = 13, true, false) 
     AS [Has Garage], 
     Iif([unitamenities].[amenityid] = 14, true, false) 
     AS [Parking Included], 
     Iif([unitamenities].[amenityid] = 15, true, false) 
     AS 
     [Parking Available for a Fee], 
     Iif([unitamenities].[amenityid] = 16, true, false) 
     AS OnBusRoute, 
     Iif([unitamenities].[amenityid] = 17, true, false) 
     AS BreakfastBar, 
     Iif([unitamenities].[amenityid] = 18, true, false) 
     AS Microwave, 
     Iif([unitamenities].[amenityid] = 19, true, false) 
     AS UndergroundParking, 
     Iif([unitamenities].[amenityid] = 20, true, false) 
     AS HeatIncluded, 
     Iif([unitamenities].[amenityid] = 21, true, false) 
     AS FencedInYard, 
     Iif([unitamenities].[amenityid] = 22, true, false) 
     AS StainlessAppliances, 
     Iif([unitamenities].[amenityid] = 23, true, false) 
     AS HasPool, 
     Iif([unitamenities].[amenityid] = 30, true, false) 
     AS Patio, 
     Iif([unitamenities].[amenityid] = 31, true, false) 
     AS Deck, 
     Iif([unitamenities].[amenityid] = 32, true, false) 
     AS Porch, 
     Iif([unitamenities].[amenityid] = 33, true, false) 
     AS FitnessCenter, 
     Iif([unitamenities].[amenityid] = 34, true, false) 
     AS CommunityRoom, 
     Iif([unitamenities].[amenityid] = 35, true, false) 
     AS Elevator, 
     Iif([unitamenities].[amenityid] = 36, true, false) 
     AS Vending, 
     Iif([unitamenities].[amenityid] = 37, true, false) 
     AS WhirlpoolTub, 
     Iif([unitamenities].[amenityid] = 38, true, false) 
     AS CentralAir, 
     Iif([unitamenities].[amenityid] = 39, true, false) 
     AS VaultedCeilings, 
     Iif([unitamenities].[amenityid] = 40, true, false) 
     AS GasFireplace, 
     Iif([unitamenities].[amenityid] = 41, true, false) 
     AS WoodburningFireplace, 
     Iif([unitamenities].[amenityid] = 42, true, false) 
     AS DecorativeFireplace, 
     Iif([unitamenities].[amenityid] = 43, true, false) 
     AS PrivateEntrance, 
     Iif([unitamenities].[amenityid] = 44, true, false) 
     AS OnSiteOffice, 
     Iif([unitamenities].[amenityid] = 45, true, false) 
     AS CableInternetIncluded, 
     Iif([unituserdefinedvalues].[userdefinedid] = 1, 
     [unituserdefinedvalues].[value], "") AS Beds, 
     Iif([unituserdefinedvalues].[userdefinedid] = 2, 
     [unituserdefinedvalues].[value], "") AS Baths, 
     Iif([unituserdefinedvalues].[userdefinedid] = 3, 
     [unituserdefinedvalues].[value], "") AS DenLoft, 
     Iif([unituserdefinedvalues].[userdefinedid] = 4, 
     [unituserdefinedvalues].[value], "") AS Description, 
     Iif([unituserdefinedvalues].[userdefinedid] = 5, 
     [unituserdefinedvalues].[value], "") AS WebID, 
     Iif([unituserdefinedvalues].[userdefinedid] = 7, 
     [unituserdefinedvalues].[value], "") AS Location, 
     Iif([unituserdefinedvalues].[userdefinedid] = 18, 
     [unituserdefinedvalues].[value], "") 
     AS FurnitureAvailable, 
     Iif([unituserdefinedvalues].[userdefinedid] = 21, 
     [unituserdefinedvalues].[value], "") 
     AS Landlord, 
     Iif([unituserdefinedvalues].[userdefinedid] = 22, 
     [unituserdefinedvalues].[value], "") 
     AS LaundryDetails, 
     Iif([unituserdefinedvalues].[userdefinedid] = 23, 
     [unituserdefinedvalues].[value], "") 
     AS LaundryAmt, 
     Iif([unituserdefinedvalues].[userdefinedid] = 24, 
     [unituserdefinedvalues].[value], "") 
     AS ParkingDetails, 
     Iif([unituserdefinedvalues].[userdefinedid] = 29, 
     [unituserdefinedvalues].[value], "") 
     AS Pets, 
     Iif([unituserdefinedvalues].[userdefinedid] = 33, 
     [unituserdefinedvalues].[value], "") 
     AS Schools, 
     Iif([unituserdefinedvalues].[userdefinedid] = 47, 
     [unituserdefinedvalues].[value], "") 
     AS Utilities, 
     Iif([unituserdefinedvalues].[userdefinedid] = 57, 
     [unituserdefinedvalues].[value], "") 
     AS Latitude, 
     Iif([unituserdefinedvalues].[userdefinedid] = 58, 
     [unituserdefinedvalues].[value], "") 
     AS Longitude, 
     Iif([unituserdefinedvalues].[userdefinedid] = 59, 
     [unituserdefinedvalues].[value], "") 
     AS PicSm, 
     Iif([unituserdefinedvalues].[userdefinedid] = 60, 
     [unituserdefinedvalues].[value], "") 
     AS PicLg, 
     Iif([unituserdefinedvalues].[userdefinedid] = 61, 
     [unituserdefinedvalues].[value], "") 
     AS Floorplan, 
     Iif([unituserdefinedvalues].[userdefinedid] = 62, 
     [unituserdefinedvalues].[value], "") 
     AS IsSmokeFree, 
     Iif([unituserdefinedvalues].[userdefinedid] = 77, 
     [unituserdefinedvalues].[value], "") 
     AS Manager, 
     Iif([unituserdefinedvalues].[userdefinedid] = 71, 
     [unituserdefinedvalues].[value], "") 
     AS AvailNow, 
     Iif([unituserdefinedvalues].[userdefinedid] = 72, 
     [unituserdefinedvalues].[value], "") 
     AS AvailAug, 
     Iif([unituserdefinedvalues].[userdefinedid] = 73, 
     [unituserdefinedvalues].[value], "") 
     AS AvailShortTerm, 
     Iif([unituserdefinedvalues].[userdefinedid] = 74, 
     [unituserdefinedvalues].[value], "") 
     AS AvailSublet, 
     Iif([unituserdefinedvalues].[userdefinedid] = 75, 
     [unituserdefinedvalues].[value], "") 
     AS AvailFrom, 
     Iif([unituserdefinedvalues].[userdefinedid] = 76, 
     [unituserdefinedvalues].[value], "") 
     AS AvailTo 
FROM ((units 
     INNER JOIN unitamenities 
       ON units.unitid = unitamenities.unitid) 
     INNER JOIN unittypes 
       ON units.unittypeid = unittypes.unittypeid) 
     INNER JOIN unituserdefinedvalues 
       ON units.unitid = unituserdefinedvalues.unitid; 
+0

呵呵。非常有趣。這也是M $ SQL,對吧? – Strawberry

+1

你有沒有考慮把'Pool'和'Park'這樣的東西存放在'Unit'表中的'BOOL'變量?你爲什麼要在應用層應該完成的SQL表中做這麼多工作?這是一個非常不切實際的方法。如果您需要有將來可能擴展的多種設施,請嘗試使用「單元」表格,「標籤」表格和「價值」表格。 – sjagr

+0

這是M $ SQL,但我很樂意歡迎MySQL。我從一個只讀的MySQL數據庫中提取數據,在Access中創建查詢視圖,然後將它們複製並粘貼到我的網頁代碼中。 – Alex

回答

1

您正在爲自己創造很多工作。

嘗試Labels表這樣的(這將包括用戶定義的值):

LabelID | AmenityDesc 
---------|----------------- 
    1  | Pool 
    2  | Parking 
    3  | Includes Electric 
    4  | Has Parking 
    5  | Show Online 

Options表:

UnitID | LabelID | OptionValue 
---------|------------------------- 
    1  | 1  | 1 
    1  | 2  | 1 
    1  | 3  | 1 
    1  | 4  | 1 
    1  | 5  | 1 
    2  | 2  | 1 
    2  | 5  | 0 

等等等等然後加入它一起與查詢

根據您的編輯,這裏有一種方法來解決應用層中的東西(僞代碼):

$results := sql('SELECT * FROM Amenities WHERE UnitID = ?'); 
$unit[?]['amenities'] := array(); 
foreach ($results as $result): 
    $unit[?]['amenities'][] = $result['AmenityDesc']; 
endfor 

要檢查的單位有一些設施:

if ('amenity' is in $unit[?]['amenities']): 
    // do stuff 
endif 
+0

問題是我正在訪問第三方只讀數據庫。所以我不能創建額外的查詢或新的表格。我必須創建一個查詢來使用一個Select語句來提取所有數據。否則,我會創建一些查詢,然後將它們組合起來。 – Alex

+0

好吧,等待編輯 – sjagr

+0

看到我的編輯,有一種方法可以解決它。我真的會避免使用大的SQL查詢,否則如果將來發生任何變化,你將面臨一場噩夢。 – sjagr