SQL 子选择作为字段?
我在这里有点迷失...
我有几个表想从中提取统一记录:Unit
、Building
、Owner
和图片
。
到目前为止,这是我的查询:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我理解正确的话,您希望在结果集中有 4 组图片列 - 4 个类别之一?现在你前面只有一张,对吧?
您可以使用不同的别名和不同的连接子句多次连接到同一个表。只需加入 4D_Building_Picts 4 次,每张您想要的图片一次。
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.
我认为你想要合并功能。它需要多个字段,并返回其中第一个非空字段。所以就像这样:
尽管如此,您最终只能得到一个值,但这实际上可能不是您想要的。如果您想要所有这些,我建议使用相关子查询,如下所示:
I think you want the coalesce function. It takes multiple fields, and returns the first of them that's non-null. So something like:
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: