带有查询变量的MySQL SubQuery

时间:2012-08-21 04:04:45

标签: mysql sql

我正在尝试执行以下MySql查询,当我去运行它时,它告诉我子查询中的“q.object_id”是未知的(如果我将其更改为o.id,它表示相同)。

SELECT q.*, (SELECT c.title 
             FROM api_course c 
             LEFT OUTER JOIN api_object_parents op 
                 ON c.object_id = op.parent 
                     AND op.object_id = q.object_id) as parent_title
    FROM api_quiz q
    LEFT OUTER JOIN api_object o ON q.object_id = o.id
    WHERE o.type = 'Quiz'

基本上我需要获得在主查询中使用的相同ID并在子查询中使用它。

谢谢!

1 个答案:

答案 0 :(得分:4)

这就是你要找的东西吗?

SELECT  a.*, d.title
FROM    api_quiz a
            LEFT JOIN   api_object b
                on a.object_id = b.id
            LEFT JOIN   api_object_parents c
                ON c.parent = a.object_id
            LEFT JOIN   api_course d 
                ON c.object_id = c.parent