mysql——电影的国别分析

1tuwyuhd  于 2021-08-09  发布在  Java
关注(0)|答案(5)|浏览(319)

写一个查询来查找每个国家的电影出租数量。仅显示至少租用了一部电影的国家。按字母顺序排列这些国家。
数据库:https://dev.mysql.com/doc/sakila/en/sakila-structure.html
下面是编写的代码

  1. select e.country, count(a.rental_id) as rental_count
  2. from rental a
  3. left outer join customer b ON (a.customer_id=b.customer_id)
  4. left outer join address c ON (b.address_id=c.address_id)
  5. left outer join city d ON (c.city_id=d.city_id)
  6. left outer join country e ON (d.country_id=e.country_id)
  7. group by country
  8. order by rental_count >=1

我的输出显示如下

  1. ***************************1. row***************************
  2. Angola
  3. 4
  4. ***************************2. row***************************
  5. American Samoa
  6. 4
  7. ***************************3. row***************************
  8. Afghanistan
  9. 4
  10. ***************************4. row***************************
  11. Algeria
  12. 2
  13. ***************************5. row***************************
  14. New Zealand
  15. 1

如何将输出转换为字母顺序

gr8qqesn

gr8qqesn1#

你可能很困惑 ORDER BY 以及 HAVING .
仅显示至少租用了一部电影的国家。
这就是你要做的:

  1. order by rental_count >=1

但要对聚合列进行筛选,应使用having子句:

  1. having rental_count >=1

但是,您可以通过使用 INNER JOIN 而不是 LEFT OUTER JOIN . 那么在这种情况下就不需要having子句了。
ORDERBY子句用于按特定顺序获取结果。如果希望按字母顺序对国家进行排序,可以使用

  1. order by e.country
gev0vcfq

gev0vcfq2#

我猜您是想按国家名称acc\ U account\ U transactions对结果进行排序,而不是按计数

  1. order by e.country
smdnsysy

smdnsysy3#

  1. use upgrad;
  2. select Country, count(rental_id) as Rental_count
  3. from rental
  4. inner join customer
  5. using (customer_id)
  6. inner join address
  7. using (address_id)
  8. inner join city
  9. using (city_id)
  10. inner join country
  11. using (country_id)
  12. group by country
  13. order by country;
62o28rlo

62o28rlo4#

  1. select country,count(rental_id) from country
  2. inner join city using (country_id)
  3. inner join address using (city_id)
  4. inner join customer using (address_id)
  5. inner join rental using (customer_id)
  6. group by country
  7. having count(rental_id)>0
  8. order by country asc;
9njqaruj

9njqaruj5#

  1. select country, count(rental_id>=1) as rental_count
  2. from rental
  3. join customer using (customer_id)
  4. join address using (address_id)
  5. join city using (city_id)
  6. join country using (country_id)
  7. group by country
  8. order by country desc;

相关问题