“最长的电影租赁时间和租用该电影的客户”MySQL Sakila查询

时间:2016-04-02 10:21:30

标签: mysql

我正在尝试编写一个可以返回最长电影租赁时间的查询以及在Sakila数据库中租借上述电影(以及电影名称)的客户。

作为第一步,我编写了以下查询,该查询应该返回film_id,inventory_id,customer_id和最长租期(以周为单位):

SELECT DDIFF.*

FROM (SELECT rental.inventory_id, rental.customer_id, DATEDIFF(rental.return_date, rental.rental_date)/7 AS TM FROM rental) AS DDIFF

INNER JOIN  (SELECT  DDIFF.inventory_id, DDIFF.customer_id, MAX(DDIFF.TM) AS WEEKS 
FROM (SELECT  rental.inventory_id, rental.customer_id, DATEDIFF(rental.return_date, rental.rental_date)/7 AS TM FROM rental) AS DDIFF
GROUP BY DDIFF.inventory_id, DDIFF.customer_id) AS MXLST 

ON  DDIFF.TM = MXLST.WEEKS AND  MXLST.inventory_id = DDIFF.inventory_id AND MXLST.customer_id = DDIFF.customer_id

;

但是我得到了一些意想不到的结果(它应该返回多个行,最长持续时间为1.4286周,但由于某种原因我得到1.0周)。我究竟做错了什么?听到解释,我将非常感谢。

请注意,我已尝试按照thisthis正确使用MAX()但仍未返回正确的结果。

编辑:Sakila tables`结构可以找到here

2 个答案:

答案 0 :(得分:1)

您的查询效率非常低:您在完整表格上执行3 SELECT次,然后执行其他操作。我测试了你的查询:它已经运行超过60秒而没有给出任何结果。刚杀死它并重写您的查询如下:

SELECT c.first_name, c.last_name, f.title, DATEDIFF(r.return_date,r.rental_date)/7 AS rental_time 
FROM rental AS r
INNER JOIN inventory AS i 
ON r.inventory_id = i.inventory_id
INNER JOIN film AS f
ON i.film_id = f.film_id
INNER JOIN customer AS c
ON r.customer_id = c.customer_id
HAVING rental_time = (SELECT MAX(DATEDIFF(return_date,rental_date))/7 FROM rental)

答案 1 :(得分:0)

似乎我设法解决了我自己的问题。这是一个更新的查询以供将来参考:

SELECT customer.first_name,
       customer.last_name,
       film.title,
       DDIFF.TM AS 'rental duration'
FROM customer,
     film,
     (SELECT inventory.film_id,
             rental.inventory_id,
             rental.customer_id,
             DATEDIFF(rental.return_date, rental.rental_date)/7 AS TM
      FROM rental,
           inventory
      WHERE rental.inventory_id = inventory.inventory_id) AS DDIFF

INNER JOIN (SELECT DDIFF.film_id,
              DDIFF.inventory_id,
              DDIFF.customer_id,
              MAX(DDIFF.TM) AS WEEKS 
            FROM (SELECT inventory.film_id,
                         rental.inventory_id,
                         rental.customer_id,
                         DATEDIFF(rental.return_date, rental.rental_date)/7 AS TM
                  FROM rental,
                       inventory
                  WHERE rental.inventory_id = inventory.inventory_id) AS DDIFF
            GROUP BY DDIFF.film_id,
                     DDIFF.inventory_id,
                     DDIFF.customer_id) AS MXLST 
ON  DDIFF.TM = (SELECT MAX(DDIFF.TM)
                FROM (SELECT inventory.film_id,
                             rental.inventory_id,
                             rental.customer_id,
                             DATEDIFF(rental.return_date, rental.rental_date)/7 AS TM
                      FROM rental,
                           inventory
                      WHERE rental.inventory_id = inventory.inventory_id) AS DDIFF)
AND MXLST.film_id = DDIFF.film_id
AND MXLST.inventory_id = DDIFF.inventory_id
AND MXLST.customer_id = DDIFF.customer_id

WHERE customer.customer_id = DDIFF.customer_id
AND film.film_id = DDIFF.film_id;

我承认,它效率不高也不合适,但这是一个正确的答案。欢迎任何人尝试找到更有效/可读的查询,返回相同的结果。无论如何,感谢所有试图提供帮助的人。