需要 mysql 查询帮助

发布于 2024-10-16 10:31:35 字数 1377 浏览 2 评论 0原文

第一段背景信息:这是关于 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 技术交流群。

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

发布评论

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

评论(3

叹倦 2024-10-23 10:31:35

正如您已经发现的,问题在于您尝试在子查询的约束中使用外部查询中的列:

  (SELECT sort_order
      from object_object
      WHERE object_id = (SELECT parent_id
                         from object_object
                         WHERE object_id = obj_asset.object_id )
     )

可以重写此子查询,但仅查看原始查询时尚不清楚如何重写。

您的原始查询(已重新格式化)中可能存在错误:

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;

名为 op 的子查询未在任何联接或 where 子句中使用。

我的最佳猜测是您想要执行以下操作:

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, o2.object_id
    from object_object as o1
    INNER JOIN object_object as o2 ON o1.object_id = o2.parent_id
    ) AS op ON obj_asset.object_id = op.object_id
WHERE obj_asset.profile_id = 140 AND obj_asset.rsvp_enabled = 1 AND object.is_published = 1 ORDER BY sort DESC;

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:

  (SELECT sort_order
      from object_object
      WHERE object_id = (SELECT parent_id
                         from object_object
                         WHERE object_id = obj_asset.object_id )
     )

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):

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;

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:

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, o2.object_id
    from object_object as o1
    INNER JOIN object_object as o2 ON o1.object_id = o2.parent_id
    ) AS op ON obj_asset.object_id = op.object_id
WHERE obj_asset.profile_id = 140 AND obj_asset.rsvp_enabled = 1 AND object.is_published = 1 ORDER BY sort DESC;
不必你懂 2024-10-23 10:31:35

我认为你需要输入这个(包括 obj_asset 到 FROM):

(SELECT parent_id FROM object_object, obj_asset  WHERE object_object.object_id = obj_asset.object_id )

但不是这个:

(SELECT parent_id from object_object WHERE object_id = obj_asset.object_id )

I think you need to type this (include obj_asset to FROM):

(SELECT parent_id FROM object_object, obj_asset  WHERE object_object.object_id = obj_asset.object_id )

but not this:

(SELECT parent_id from object_object WHERE object_id = obj_asset.object_id )
梦与时光遇 2024-10-23 10:31:35

您的子查询在 FROM 子句中不包含 obj_asset。

(SELECT parent_id FROM object_object WHERE object_id = obj_asset.object_id )

让这部分运行起来,你应该对整个事情有更好的运气。

只是一个提示...如果您在 SQL 语句中添加更多换行符和缩进,您将更容易发现问题。

Your subquery doesn't include obj_asset in the FROM clause.

(SELECT parent_id FROM object_object WHERE object_id = obj_asset.object_id )

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.

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