简体   繁体   中英

Update query does not recognize the fields of the table it is run on

I have a table named history with fields:

id, accessionNumber, date_borrowed, date_returned, fine

I intend to update the table's row in the following manner when the query is run:

1- insert today's date into return_date

2- subtract the borrow_date from return_date(=today's date), multiply by a constant that I am going to send as a parameter and insert the result into fine

I have been looking around for hours now, but as I'm a beginner I couldn't get much out of it.

This is my query that doesn't work, what am I doing wrong?

UPDATE history
SET date_returned = CURDATE() ,
fine=DATEDIFF(CURDATE(),STR_TO_DATE(history.date_borrowed, '%m/%d/%Y')) * @some_constant
WHERE id = @id and accessionNumber=@accessionNumber

I am using MySQL Workbench 6 and C# if that's is going to help.

The error message I am getting is :

Incorrect datetime value: '2013-12-11' for function str_to_date

Thanks in advance for any help.

I am going to guess that borrow_date is really stored as a date and not as a string. If so, then the query can be simplified to:

UPDATE history
    SET date_returned = CURDATE() ,
        fine = DATEDIFF(CURDATE(), borrow_date) * some_constant
    WHERE id = @id and accessionNumber=@accessionNumber;

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