开发者

SQL Sub-select as field?

I'm a bit lost here...

I have several tables I'd like to pull a unified record from: Unit, Building, Owner, and Picture.

Here's my query so far:

SELECT building.`Street_Address`
    , building.`Building_Name`
    , building.`Building_Type`
    , CONCAT(building.`Cross_Street_1`, ' & ', building.`Cross_Street_2`) Cross_Streets
    , building.`Cross_Street_1`
    , building.`Cross_Street_2`
    , building.`Access` Building_Access
    , owner.`Company_Name`
    , owner.`Contact_Or_Reference`
    , owner.`Landlord_Phone`
    , picture.`Path_To_Picture_On_Server`
    , picture.`Picture_Category`
    , unit.`Apartment_Number`
    , unit.`Unit_Size_Number` Size
    , unit.`Is_Doorman`
    , unit.`Is_Furnished`
    , unit.`Is_Elevator`
    , unit.`Is_Pets`
    , unit.`Is_OutdoorSpace`
    , unit.`Rent_Price`
    , unit.`Baths`
    , unit.`Access` Unit_Access
    , unit.`fourd_id`
    , unit.`Updated_Date`
    , unit.`Occupancy_Date`
    , unit.`Term`
    , unit.`Incentives`
    , unit.`Info_OutdoorSpace`
    , unit.`List_Date`
    , zone.`Description`
FROM 4D_Units unit
JOIN 4D_Building building
    ON unit.`BUILDING_RecID` = building.`fourd_id`
JOIN 4D_Zones zone
    ON building.`ZONES_RecID` = zone.`fourd_id`
LEFT JOIN 4D_Owners owner
    ON unit.`OWNER_RecID` = owner.`fourd_id`
LEFT JOIN 4D_Building_Picts picture
    ON (building.`fourd_id` = picture.`BUILDING_RecID` AND picture.`Picture_Category` = 'Front')
WHERE unit.`id` = 49901

This works fine as-is, except that the return record will only ever have the "Front" picture in the record (if present). My issue is that there are several different types of photos that could be associated with a开发者_运维技巧 return record, including 'Panorama', 'Interior', and 'Floorplan'... all are different possible values for picture.Picture_Category.

Is there a way to return those values (if they are present, as above) in the returned set without doing a separate query? I want the returned set to include (if present) aliased values for all four possible options of picture.Picture_Category: 'Front', 'Panorama', 'Interior', & 'Floorplan' (with their own unique picture.Path_To_Picture_On_Server associated with it).

Does that make sense?


If I understand you correctly, you want to have 4 sets of picture columns in your result set - one of reach of 4 categories? Right now you have just one for front, right?

You can join to the same table multiple times with different aliases and different join clauses. Just join to 4D_Building_Picts 4 times, once for each picture you want.

select 
    --whatever
    , pic_front.`Path_To_Picture_On_Server` AS Front_Path_To_Picture_On_Server
    , pic_panorama.`Path_To_Picture_On_Server` AS Panorama_Path_To_Picture_On_Server
    , pic_interior.`Path_To_Picture_On_Server` AS Interior_Path_To_Picture_On_Server
    , pic_floorplan.`Path_To_Picture_On_Server` AS Floorplan_Path_To_Picture_On_Server
    --whatever
FROM 4D_Units unit
JOIN 4D_Building building
    ON unit.`BUILDING_RecID` = building.`fourd_id`
JOIN 4D_Zones zone
    ON building.`ZONES_RecID` = zone.`fourd_id`
LEFT JOIN 4D_Owners owner
    ON unit.`OWNER_RecID` = owner.`fourd_id`
LEFT JOIN 4D_Building_Picts pic_front
    ON (building.`fourd_id` = pic_front.`BUILDING_RecID` AND pic_front.`Picture_Category` = 'Front')
LEFT JOIN 4D_Building_Picts pic_panorama
    ON (building.`fourd_id` = pic_panorama.`BUILDING_RecID` AND pic_panorama.`Picture_Category` = 'Panorama')
LEFT JOIN 4D_Building_Picts pic_interior
    ON (building.`fourd_id` = pic_interior.`BUILDING_RecID` AND pic_interior.`Picture_Category` = 'Interior')
LEFT JOIN 4D_Building_Picts pic_floorplan
    ON (building.`fourd_id` = pic_floorplan.`BUILDING_RecID` AND pic_floorplan.`Picture_Category` = 'Floorplan')
WHERE unit.`id` = 49901


I think you want the coalesce function. It takes multiple fields, and returns the first of them that's non-null. So something like:

Select
  Coalesce(A.Panorama, A.Interior, A.Floorplan, '') as ImagePath
From
  Table A

You only end up with one value this way though, which may not actually be what you're after. If you want all of them I'd suggest using correlated subqueries, like so:

Select
  (Select P.Path_To_Picture From 4D_Building_Picts P where P.Building_RecID = B.fourd_Id And P.Picture_Category = 'Front') as Front_Pic,
  (Select P.Path_To_Picture From 4D_Building_Picts P where P.Building_RecID = B.fourd_Id And P.Picture_Category = 'Panorama') as Panamora_Pic,
  (Select P.Path_To_Picture From 4D_Building_Picts P where P.Building_RecID = B.fourd_Id And P.Picture_Category = 'FloorPlan') as FloorPlan_Pic,
  ...
From
  4D_Building B
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜