【问题标题】:Write a query to find the full names of those customers who have rented at least one movie and belong to the city Arlington编写查询以查找至少租用一部电影并属于阿灵顿市的客户的全名
【发布时间】:2020-06-14 05:57:10
【问题描述】:

我收到一条错误消息,提示输出与给定问题不匹配

萨基拉 DB:https://dev.mysql.com/doc/sakila/en/sakila-structure.html

下面是我的代码

with temp as (
select concat(b.first_name, ' ',b.last_name) as name,
count(a.rental_id >= 1) as rental_count
from rental a
inner join customer b ON   (a.customer_id=b.customer_id)
inner join address c ON    (b.address_id=c.address_id)
inner join city d ON       (c.city_id=d.city_id)
where d.city like "Arlington"
group by name
order by rental_count
)
select name from temp

预期结果是

full names of those customers who have rented at least one movie and belong to the city Arlington.

请告诉我是什么错误或有其他方法吗?

【问题讨论】:

  • 在您的数据中,如果某人没有租借任何电影,他们如何成为客户?

标签: mysql sql inner-join


【解决方案1】:

您可以使用having 子句,尝试以下操作

select 
    concat(b.first_name, ' ',b.last_name) as name
from rental a
inner join customer b 
on a.customer_id = b.customer_id
inner join address c    
on b.address_id = c.address_id
inner join city d       
on c.city_id = d.city_id
where d.city = 'Arlington'
group by 
    concat(b.first_name, ' ',b.last_name)
having count(a.rental_id) >= 1

【讨论】:

  • 得到错误 >> 第 4 行的错误 1064 (42000):您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取正确的语法,以便在第 3 行的“从出租内部连接客户 b ON a.customer_id = b.customer_id 内部连接”附近使用
  • @PoornimaKeshavaiah 现在试试,没有必要的,
【解决方案2】:
select concat(first_name," ", last_name) as Customer_name
from rental
inner join customer
using (customer_id)
inner join address
using(address_id)
inner join city
using (city_id)
where city = "Arlington"
group by Customer_name
having count(rental_id) > 0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-24
    • 1970-01-01
    • 2020-10-03
    • 2021-03-03
    • 2014-11-24
    • 2020-11-22
    • 2015-10-13
    • 1970-01-01
    相关资源
    最近更新 更多