第一段背景信息:这是关于 cms,它保存对象-对象关系及其在单个表中的排序,列是 object_id、parent_id 和排序顺序
第二件有几个连接的查询,我想按 2 个参数排序。其中之一是对象本身的排序,第二个是其父对象的排序顺序。
我现在的查询是:
SELECT obj_asset.*, object.headline AS title, oo.sort_order AS sort, op.sort_order FROM obj_asset
JOIN object ON obj_asset.object_id = object.object_id
JOIN object_object AS oo on obj_asset.object_id = oo.object_id
JOIN (SELECT sort_order from object_object WHERE object_id = (SELECT parent_id from object_object WHERE object_id = obj_asset.object_id )) AS op ON obj_asset.object_id = oo.object_id
WHERE obj_asset.profile_id = 140 AND obj_asset.rsvp_enabled = 1 AND object.is_published = 1 ORDER BY sort DESC;
它不起作用。不过,这很好用:
SELECT obj_asset.*, object.headline AS title, oo.sort_order AS sort, op.sort_order FROM obj_asset
JOIN object ON obj_asset.object_id = object.object_id
JOIN object_object AS oo on obj_asset.object_id = oo.object_id
JOIN (SELECT sort_order from object_object WHERE object_id = (SELECT parent_id from object_object WHERE object_id = 11111 )) AS op ON obj_asset.object_id = oo.object_id
WHERE obj_asset.profile_id = 140 AND obj_asset.rsvp_enabled = 1 AND object.is_published = 1 ORDER BY sort DESC;
我得到的错误是:
1054 - 'where 子句'中的未知列'obj_asset.object_id'
我怎样才能让它工作?
谢谢!
编辑:如果我能想出替代方法将父母排序到查询中,我可以解决这个问题。有没有这样的方法?