这是我的查询

SELECT *
FROM personal_store_application a, personal_store_application_lang b, (

  SELECT SUM( total ) total
  FROM (

     SELECT COUNT( NPID ) total
     FROM actnews
     WHERE `NPID` = a.PID
     UNION ALL SELECT COUNT( NPID ) total
     FROM logistics
     WHERE `NPID` = a.PID
  ) AS s
) AS x
WHERE a.Sn = b.Sn
AND b.Lang = 'zh-tw'
AND Application != 'S'
ORDER BY a.C_TIME DESC


该查询在第二级Unknown column 'a.PID' in 'where clause'中中断
我无法在第二级子查询中访问别名a。如何实现呢?如果将a.PID更改为静态PID,则查询工作正常。

提前致谢 :)

最佳答案

在执行子查询时,最先执行的是最里面的查询,在这种情况下SELECT COUNT( NPID ) total FROM actnews WHERE NPID = a.PID UNION ALL SELECT COUNT( NPID ) total FROM logistics WHERE NPID = a.PID

它不知道什么是a.,因为它是在外部查询中声明的,因此是错误。

09-27 19:18