SQL问题请教# Database - 数据库
b*s
1 楼
有两个表,一个movies,有id, title, year_released
另一个sale表,有movie_id, sale,theatre
现在要得到各movie(title)卖票的总额,然后按year_released排序
而movie可能在多个theatre里放映,在sale表里有多条记录
也可能没有放映,在sale里没有记录,那么就是0
怎么用一个SQL写呢?
我试了
SELECT title, sum(sale) FROM movies, sale
WHERE movies.id=sale.movie_id
GROUP BY title
UNION
SELECT title, 0
FROM movies
WHERE movies.id NOT IN (SELECT movie_id FROM sale)
ORDER BY movies.year_released;
但是通不过,报告说ODER BY year_released这里不对
另一个sale表,有movie_id, sale,theatre
现在要得到各movie(title)卖票的总额,然后按year_released排序
而movie可能在多个theatre里放映,在sale表里有多条记录
也可能没有放映,在sale里没有记录,那么就是0
怎么用一个SQL写呢?
我试了
SELECT title, sum(sale) FROM movies, sale
WHERE movies.id=sale.movie_id
GROUP BY title
UNION
SELECT title, 0
FROM movies
WHERE movies.id NOT IN (SELECT movie_id FROM sale)
ORDER BY movies.year_released;
但是通不过,报告说ODER BY year_released这里不对