简体   繁体   English

SQL:按未连接的表分组

[英]SQL: Group by Tables that are not connected

I would like to display a list of movie titles that have been rented more than once at a DVD store. 我想显示在DVD商店中租借不止一次的电影标题的列表。 The 'rental' and 'film' tables are not connected though as you can see here: http://www.postgresqltutorial.com/wp-content/uploads/2013/05/PostgreSQL-Sample-Database.png 虽然您可以在此处看到“租金”表和“电影”表未连接的情况: http : //www.postgresqltutorial.com/wp-content/uploads/2013/05/PostgreSQL-Sample-Database.png

Here is what I have so far, which gives the error 'rentCount does not exist' 这是我到目前为止的内容,它给出错误“ rentCount不存在”

SELECT title, COUNT(rental_id) AS rentCount
FROM film f, rental r
WHERE rentCount >= 1
GROUP BY title
ORDER BY rentCount

(Unsure what kind of SQL you're using, however in SQL or MySQL, this should work) (不确定您使用的是哪种SQL,但是在SQL或MySQL中,这应该可行)

It seems that the link from your film to rental table is through your inventory table. filmrental表的链接似乎是通过inventory表。 Join film to inventory using film_id , then join inventory to rental using inventory_id . 使用film_id film加入inventory ,然后使用inventory_id film_idinventory加入rental

SELECT title, COUNT(rental_id) AS rentCount
FROM film f
INNER JOIN inventory i ON i.film_id = f.film_id
INNER JOIN rental r ON i.inventory_id = r.inventory_id
HAVING rentCount >= 1
GROUP BY title
ORDER BY rentCount

EDIT: Thanks to Nick's point out in the comments, I've changed your WHERE to be a HAVING , the reason being that you're using an aggregate function column ( rentCount ). 编辑:由于尼克在评论中指出,我将您的WHERE更改为HAVING ,原因是您使用的是聚合函数列( rentCount )。

You need a HAVING clause: 您需要一个HAVING子句:

SELECT f.title, COUNT(*) AS rentCount
FROM film f
INNER JOIN inventory i 
ON i.film_id = f.film_id
INNER JOIN rental r 
ON i.inventory_id = r.inventory_id
GROUP BY f.title
HAVING COUNT(*)  > 1
ORDER BY f.title

Notice I did not reference the alias "rentCount" in the HAVING. 注意,我没有在HAVING中引用别名“ rentCount”。

SQL will not allow to use the use the column alias in the filter condition of the same query.if you wanted to use the column name, you need to use as a subquery like below. SQL不允许在同一查询的过滤条件中使用使用列别名。如果要使用列名,则需要将其用作子查询,如下所示。

SELECT *
 FROM
   (SELECT title, COUNT(r.rental_id) AS rentCount
  FROM film f
      JOIN Inventory I on f.film_id=I.film_id
    JOIN rental r on l.Inventory_id= r.Inventory_id
  GROUP BY title)t
  WHERE rentCount >= 1
  ORDER BY rentCount

You can also use a HAVING clause if you are using an agrgregate function and wanted to make the filter within the query. 如果您正在使用聚集函数并希望在查询中进行过滤,则还可以使用HAVING子句。

  SELECT *
 FROM
   (SELECT title, COUNT(r.rental_id) AS rentCount
  FROM film f
      JOIN Inventory I on f.film_id=I.film_id
    JOIN rental r on l.inventory_id= r.inventory_id
  GROUP BY title
   Having Count(rental_id)>=1)t
  ORDER BY rentCount

Always keep in mind that Never use commas to link or make relation between the tables.Write appropriate joins and link them.In your case if you are using INENER JOIN there is no need of the filer condition 'Where rentCount>=1' since the condition will be always return a true value. 请记住,切勿使用逗号链接或在表之间建立联系。编写适当的联接并将它们链接起来。在您的情况下,如果使用INENER JOIN,则不需要文件条件'Where rentCount> = 1',因为条件将始终返回真实值。 I mean the below query will give you the same result like the others. 我的意思是以下查询将为您提供与其他查询相同的结果。

 SELECT title, COUNT(r.rental_id) AS rentCount
  FROM film f
      JOIN Inventory I on f.film_id=I.film_id
      JOIN rental r on l.inventory_id= r.inventory_id
  GROUP BY title

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM