具有对角线结果的一对多SQL查询

时间:2013-12-23 23:45:47

标签: mysql sql sql-server

我有三张桌子。这是住房业。所以我有一个Unit表,一个Amenities表和一个UserDefined表。

对于单位表中的每个条目,我想在一行中显示所有便利设施和用户定义的值。

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;

1 个答案:

答案 0 :(得分: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