需要 mysql 查询帮助
第一段背景信息:这是关于 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”
我怎样才能让它工作?
谢谢!
编辑:如果我能想出替代方法将父母排序到查询中,我可以解决这个问题。有这样的办法吗?
1st piece of background inf : This is about cms which holds object - object relations and their sorting in single table, columns are object_id, parent_id and sorting order
2nd piece have query with several joins, which i want to sort by 2 parameters. One of those is sorting of the object itself and 2nd is the sorting order of its parent.
The query i have for now is:
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;
And it does not work. This works fine though:
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;
The error i get is:
1054 - Unknown column 'obj_asset.object_id' in 'where clause'
How can i get it working?
Thanks!
EDIT: i could get around the problem, if i could come up with alternate way to include parents sorting into the query. IS there such a way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如您已经发现的,问题在于您尝试在子查询的约束中使用外部查询中的列:
可以重写此子查询,但仅查看原始查询时尚不清楚如何重写。
您的原始查询(已重新格式化)中可能存在错误:
名为
op
的子查询未在任何联接或 where 子句中使用。我的最佳猜测是您想要执行以下操作:
As you already figured out, the problem is that you are trying to use a column from an outer query in a constraint of a subquery:
This subquery can be rewritten, but it is not clear how when only looking at your original query.
There is probably an error in your original query (reformatted):
The sub query named
op
is not used in any join or where clause.My best guess is that you wanted to to the following:
我认为你需要输入这个(包括 obj_asset 到 FROM):
但不是这个:
I think you need to type this (include obj_asset to FROM):
but not this:
您的子查询在 FROM 子句中不包含 obj_asset。
让这部分运行起来,你应该对整个事情有更好的运气。
只是一个提示...如果您在 SQL 语句中添加更多换行符和缩进,您将更容易发现问题。
Your subquery doesn't include obj_asset in the FROM clause.
Get that part running, and you should have better luck with the whole thing.
Just a hint... if you put some more newlines and indentations in your SQL statements it will be easier for you to spot problems.