编写一个查询来查找租用科幻电影超过5次的客户的全名把这些名字按字母顺序排列

pb3skfrl  于 2021-08-09  发布在  Java
关注(0)|答案(2)|浏览(399)

你好下面是我的代码
数据库:https://dev.mysql.com/doc/sakila/en/sakila-structure.html

select concat(first_name,' ',last_name) from
customer where customer_id in (
select customer_id from (
select customer_id, count(rental_id) as num
from 
category 
inner join film_category using(category_id) 
inner join film using(film_id) 
inner join inventory using(film_id) 
inner join rental using (inventory_id)
where name='Sci-Fi'
group by customer_id, rental_id)
where num > 5)T)

当我执行时,我得到以下错误

ERROR 1248 (42000) at line 2: Every derived table must have its own alias

预期结果是 "full names of customers who have rented sci-fi movies more than 5 times. Arrange these names in the alphabetical order" 你能告诉我我犯了什么错误吗?

svgewumm

svgewumm1#

欢迎来到so!
首先,你好像有3个开口 ( parens和4结束 ) 帕伦斯。你应该删除最后一个括号,这样你就有了平衡的括号。
之后,您需要将别名应用于最深层次的查询(类似的问题:在mysql中“每个派生表都必须有自己的别名”的错误是什么。。。

where name='Sci-Fi'
group by customer_id, rental_id)
where num > 5)T)

你可能想。。。

where name='Sci-Fi'
group by customer_id, rental_id) AS T
where num > 5)

(别忘了,不需要额外的结尾部分,所以你可以看到我删除了它。它可能是您拥有的更大查询的一部分,但对问题中的独立代码没有帮助。)
这将阻止您看到的即时错误。至少,现在在我的数据库中,我看到的错误是: ERROR 1146 (42S02): Table 'db.customer' doesn't exist .

ylamdve6

ylamdve62#

select concat(first_name, ' ', last_name) as Customer_name
from category
inner join film_category
using (category_id)
inner join film
using (film_id)
inner join inventory
using (film_id)
inner join rental
using (inventory_id)
inner join customer
using (customer_id)
where name = 'Sci-Fi'
group by Customer_name
having count(rental_id) > 3
order by Customer_name;

相关问题