如何查找過去24個月內租借最多的圖書?查詢檢索過去24個月內租借最多的圖書
rentedBooks (rentid, bookid, dateout, datein)
我已經試過如下:
SELECT * FROM RENTEDBOOKS, COUNT(BOOKID) WHERE DATEIN >= DATEADD(MONTH, -24, GETDATE());
但一直收到錯誤提無效的標識符:S
注:我不使用MySQL的!
如何查找過去24個月內租借最多的圖書?查詢檢索過去24個月內租借最多的圖書
rentedBooks (rentid, bookid, dateout, datein)
我已經試過如下:
SELECT * FROM RENTEDBOOKS, COUNT(BOOKID) WHERE DATEIN >= DATEADD(MONTH, -24, GETDATE());
但一直收到錯誤提無效的標識符:S
注:我不使用MySQL的!
我不知道是怎麼回事你的WHERE子句(DATEADD,GETDATE),但其餘的應該是這個樣子......
SELECT BOOKID, COUNT(BOOKID)
FROM RENTEDBOOKS
WHERE ...
GROUP BY BOOKID;
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;
WHERE DATEIN >= DATEADD('m',-24, Date$());
您使用了什麼樣的SQL? – Mack
您不能從'count(bookid)'中選擇表格作爲表格。你可能希望'select count(bookid)from ...' –
dateadd看起來像tsql(SQL server not oracle) – beny23