错误#1349:View的select cluse在from子句中包含一个子查询

时间:2014-05-05 07:02:10

标签: sql querying

这是我的代码:

SELECT CAST.first_name,
  CAST.last_name,
  AWARDED.castID,
  COUNT(*) AwardsWon
FROM AWARDED,
  ROLE,
  CAST
WHERE ROLE.role    = "Director"
AND ROLE.castID    = AWARDED.castID
AND ROLE.movieID   = AWARDED.movieID
AND AWARDED.castID = CAST.castID
GROUP BY castID
HAVING COUNT(*) =
  (SELECT MAX(cnt) FROM
    (SELECT COUNT(*) cnt FROM AWARDED GROUP BY CastID
    ) z
  )

当我尝试从这段代码创建一个视图时,我得到一个表单错误:#1349 view的select包含from子句中的子查询。有什么方法可以解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

声明说......

•SELECT语句不能在FROM子句中包含子查询。

您的解决方法是为每个子查询创建一个视图。

然后从您的视图中访问这些视图

答案 1 :(得分:0)

如果我没有错,你使用的是MySQL。不确定,但您的查询可以转换如下。试一试。

SELECT `CAST`.first_name,
  `CAST`.last_name,
  AWARDED.castID,
  COUNT(AWARDED.castID) AwardsWon
FROM AWARDED
JOIN ROLE
ON ROLE.movieID = AWARDED.movieID
AND ROLE.castID = AWARDED.castID
JOIN `CAST`
ON AWARDED.castID = `CAST`.castID
WHERE ROLE.role = 'Director'
GROUP BY AWARDED.castID,`CAST`.first_name,`CAST`.last_name
HAVING COUNT(*) = MAX(AwardsWon);