Search code examples
mysqlmysql-error-1054

need help with mysql query


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?


Solution

  • 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;