Mysql子查询无法识别父查询中的字段

时间:2012-04-10 12:47:03

标签: mysql subquery

我在另一个子查询中有一个子查询的查询。因此,底层子查询无法识别顶级子查询中的字段。 我怎么能解决它?

SELECT codigo, descricao 
FROM statusplano sp, previsao pr
WHERE sp.codigo = pr.status
AND pr.plano = 13
AND sp.codigo = (SELECT codigo FROM statusplano sp1 WHERE ordem = (SELECT max(sp2.ordem) FROM statusplano sp2 WHERE sp2.codigo = pr.codigo AND pr.dataPrevista <= now()))

提前致谢。

1 个答案:

答案 0 :(得分:0)

试试这个

SELECT
codigo,
descricao
FROM sp2,
previsao pr
WHERE sp.codigo = pr.status
AND pr.plano = 13
AND sp.codigo = (SELECT
                   codigo
                 FROM sp2
                 WHERE ordem = (SELECT
                                  max(sp2.ordem)
                                FROM statusplano sp2
                                WHERE sp2.codigo = pr.codigo
                                    AND pr.dataPrevista <= now()))