我有一个sql查询:
SELECT
Object.Description,
categories.Description AS ParentDescription
FROM ObjectInspectionLog oil
INNER JOIN Object ON oil.ObjectId = Object.ObjectId
INNER JOIN Object as categories ON Object.ParentId = categories.ObjectId
WHERE oil.Notes IS NULL
GROUP BY Object.Description, categories.Description
ORDER BY MIN(Object.ObjectId)
仅供参考:我的实际命名不使用name对象 ObjectInspectionLog
:
ObjectInspectionLogId JobId ObjectId Notes
3, 6669, 15, NULL
4, 6669, 2, NULL
5, 6669, 3, Rear tires worn
6, 6669, 4, NULL
7, 6669, 5, NULL
8, 6669, 14, NULL
``` `Object` ```
ObjectId ParentId Description
1, NULL, Chassis
2, 1, Front Tires
3, 1, Rear Tires
4, 1, Windshield Condition
5, 1, Headlights
13, NULL, Adaptive Equipment
14, 13, Occupied Wheelchair Lift
15, 14, Dual post lift
目前,我得到的返回结果如下:(为简洁起见删除了一些信息)
Description ParentDescription
Windshield Condition, Chassis
Headlights, Chassis
Occupied Wheelchair Lift, Adaptive Equipment
ETC
我的目标是得到一个包含父级描述(底盘、自适应设备等)的列表
我想要的是:
Description ParentDescription
Chassis, NULL
Windshield Condition, Chassis
Headlights, Chassis
Adaptive Equipment, NULL
Occupied Wheelchair Lift, Adaptive Equipment
ETC
2条答案
按热度按时间laximzn51#
如果我理解的很好,你在找这样的东西:
t40tm48m2#
无论对象是否被
ObjectInspectionLog
,例如。ObjectId
1
应该生成一个输出行。不清楚你为什么要用group by
.以下代码使用
outer
加入以更接近您可能需要的内容: