SQL 子选择作为字段?

发布于 2024-10-16 04:59:06 字数 1870 浏览 9 评论 0原文

我在这里有点迷失...

我有几个表想从中提取统一记录:UnitBuildingOwner图片

到目前为止,这是我的查询:

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

这按原样工作正常,除了返回记录在记录中仅包含“正面”图片(如果存在)。我的问题是,有几种不同类型的照片可能与返回记录相关联,包括“全景”、“室内”和“平面图”...对于 picture.Picture_Category.

有没有办法在返回的集合中返回这些值(如果它们存在,如上所述),而无需执行单独的查询?我希望返回的集合包含picture.Picture_Category所有四个可能选项的别名值(如果存在):“Front”、“Panorama”、“Interior”和“Interior”。 “平面图”(具有与其关联的唯一picture.Path_To_Picture_On_Server)。

这有道理吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

可可 2024-10-23 04:59:06

如果我理解正确的话,您希望在结果集中有 4 组图片列 - 4 个类别之一?现在你前面只有一张,对吧?

您可以使用不同的别名和不同的连接子句多次连接到同一个表。只需加入 4D_Building_Picts 4 次,每张您想要的图片一次。

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

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
夏日浅笑〃 2024-10-23 04:59:06

我认为你想要合并功能。它需要多个字段,并返回其中第一个非空字段。所以就像这样:

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

尽管如此,您最终只能得到一个值,但这实际上可能不是您想要的。如果您想要所有这些,我建议使用相关子查询,如下所示:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文