我编写了以下查询以获取max issue_id的值。但是我得到了book_id的issue_id 14.我想要的是当book_id为2时获得issue_id 16.而当book_id为3时,issue_id为15,依此类推。
SELECT issue.issue_id AS issue_id, issue.issue_date, issue.student_id, books.availability,
CASE WHEN NOW() > DATE_ADD(issue.issue_date, INTERVAL 20 DAY)
THEN 10*DATEDIFF(NOW(), DATE_ADD(issue.issue_date, INTERVAL 20 DAY))
ELSE 0 END AS fine_amount
FROM issue
INNER JOIN books
ON issue.book_id=books.book_id
WHERE books.book_id=2
HAVING MAX(issue.issue_id)
答案 0 :(得分:1)
使用ORDER BY .. LIMIT 1获得最高值
SELECT issue.issue_id AS issue_id, issue.issue_date, issue.student_id, books.availability,
CASE WHEN NOW() > DATE_ADD(issue.issue_date, INTERVAL 20 DAY)
THEN 10*DATEDIFF(NOW(), DATE_ADD(issue.issue_date, INTERVAL 20 DAY))
ELSE 0 END AS fine_amount
FROM issue
INNER JOIN books
ON issue.book_id=books.book_id
WHERE books.book_id=2
ORDER BY issue.issue_id DESC LIMIT 1