SQL使用日期到性能计算

时间:2018-09-21 17:42:46

标签: postgresql

希望你们做得好!

我想学习如何使用postgresql(我使用sakila数据库)比较日期,并且我想回答以下问题?有人可以帮忙吗?我真的不知道该怎么处理日期部分。

/* Which customers rented movies for 5 days or more? */
SELECT 
    DISTINCT 
    c.first_name || ' ' || c.last_name AS customer_full_name
FROM 
    customer c, rental r
WHERE 
    c.customer_id = r.customer_id 
    and date('2005-01-01 22:54:33') - date('2006-12-31 22:54:33') >= 5

1 个答案:

答案 0 :(得分:2)

您可以使用:

SELECT  DISTINCT 
    c.first_name || ' ' || c.last_name AS customer_full_name
FROM customer c
JOIN rental r
  ON c.customer_id = r.customer_id 
WHERE date_of_return::date - date_of_rental::date >= 5;
-- alternatively
-- WHERE date_of_return - date_of_rental >= interval '5 days'