更新所有贷款WHERE user_id =?在案例中,DATEDIFF()> 0。错误1242和1093

时间:2017-09-17 03:30:01

标签: mysql sql datediff mysql-error-1093 mysql-error-1242

我有这个查询

Update loans set fine = case 
when DATEDIFF((
(SELECT * from (SELECT loans.due_date FROM loans where users_id = 1)  AS l1)) 
,DATE(NOW())) > 0 THEN 1
ELSE fine 
END 
WHERE users_id =1;

从表格

+-------------+
| Field       |
+-------------+
| users_id    |
| books_isbn  |
| aquire_date |
| due_date    |
| fine        |
+-------------+

与用户合作只有一个'贷款',但我不知道如何让它与一个用户的几个“贷款”一起工作。 =(

1 个答案:

答案 0 :(得分:1)

我希望查询看起来像这样:

Update loans l
    set fine = 1
    where users_id = 1 and l.due_date < curdate();

在没有样本数据和期望结果的情况下找出确切的逻辑是有点棘手的。