为嵌套查询提供别名

时间:2014-05-21 10:18:43

标签: sql database sqlite

我有这样的查询

SELECT m.title
FROM Movie AS M
WHERE
(SELECT MAX(r.stars)
 FROM rating AS r
WHERE r.mID = M.mID);

我想在结果集中显示MAX(R.stars),我该怎么做?

3 个答案:

答案 0 :(得分:3)

试试这个!

SELECT m.title, MAX(r.stars)
FROM Movie AS M
 inner join rating AS r
on r.mID = M.mID
group by m.title

或喜欢旧学校方法

  SELECT m.title, MAX(r.stars)
    FROM Movie AS M,rating AS r
    where r.mID = M.mID
    group by m.title

答案 1 :(得分:1)

你可以写

SELECT m.title, (SELECT MAX(r.stars) FROM rating AS r WHERE r.mID = M.mID) As MaxStarts
FROM Movie AS M 

答案 2 :(得分:1)

如果你真的需要在结果集中显示MAX(R.stars),那么更简单的方法是使用 LEFT JOIN

  SELECT m.Title, MAX(r.stars)
  FROM Movie m
  LEFT JOIN rating r
  ON M.mID = r.mID

无需使用别名。