如何查找过去24个月内租用最多的图书?
rentedBooks (rentid, bookid, dateout, datein)
我尝试了以下内容:
SELECT * FROM RENTEDBOOKS, COUNT(BOOKID) WHERE DATEIN >= DATEADD(MONTH, -24, GETDATE());
但是继续收到提及无效标识符的错误:S
注意:我没有使用mysql!
答案 0 :(得分:2)
SELECT BOOKID, COUNT(*) cnt
FROM RENTEDBOOKS
WHERE DATEIN >= sysdate - interval '24' month
group by BOOKID
order by cnt desc;
修改强>
SELECT BOOKID, COUNT(*) cnt
FROM RENTEDBOOKS
WHERE DATEIN >= add_months(sysdate, -24)
group by BOOKID
order by cnt desc;
答案 1 :(得分:0)
我不确定您的WHERE子句(DATEADD,getdate)发生了什么,但其余部分看起来应该是这样......
SELECT BOOKID, COUNT(BOOKID)
FROM RENTEDBOOKS
WHERE ...
GROUP BY BOOKID;
答案 2 :(得分:0)
WHERE DATEIN >= DATEADD('m',-24, Date$());