简体   繁体   中英

How do I subtract two dates that are in rows (not given in the query) Mysql

Say you're designing a system for a library and you want two dates to be subtracted - the checkout date and the checkin date. I have a file with both of these dates along with the video id and customer id. I also want to sort by 7 or more days (the book is due today or past due).

I had something like this, which worked up until the date parts:

SELECT customers.first_name, customers.last_name, video_checkout.checkout_date, 
video_checkout.checkin_date FROM video_rental, customers, video_checkout 
WHERE video_checkout.checkout_date - video_checkout.checkin_date >= 7;

So I decided to try a date_sum and TIMESTAMPDIFF, but after 2 hours of Googling, it doesn't seem that date_sums or TIMESTAMPDIFFS support subtracting two dates from tables.

Is there anyway to subtract dates that are in tables? Can I create a query that will return the subtracted dates, video id and customer id for many entries?

Using Mysql Workbench 6.3.4

The dates are in the video checkout file like this:

DELETE FROM video_checkout;

INSERT INTO video_checkout VALUES (
'1',
'1',
'2015-08-20',
'2015-08-30'
);

INSERT INTO video_checkout VALUES (
'1',
'5',
'2015-08-19',
'2015-08-29'
);

In the end I want it to look something like this:

Firstname | Lastname | Due date

Something like this

SELECT customers.first_name, customers.last_name, video_checkout.checkout_date, 
video_checkout.checkin_date FROM video_rental, customers, video_checkout 
WHERE DATEDIFF (video_checkout.checkout_date, video_checkout.checkin_date) >= 7;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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