數據結構是這樣的date | not uniquser | not uniqproduct | uniq我要做這樣一個查詢取出所有用戶,最近發出的一個產品,并且這個列表是按照date排序的。比如數據如下--
date | user | product20120901 | John | A20120903 | Tom | B20120802 | John | C20120906 | John | D20120821 | Tom | E20120902 | Tom | F20120825 | Tom | G我希望得到數據--
date | user | product | count20120906 | John | D | 320120903 | Tom | B | 4我寫的SQL如下:Cursor cursor = db.query( /* FROM */ "products", /* SELECT */ new String[]{ "*", "COUNT(product) AS count" }, /* WHERE */ null, /* WHERE args */ null, /* GROUP BY */ "user", /* HAVING */ null, /* ORDER BY */ "date DESC");
1 回答

鴻蒙傳說
TA貢獻1865條經驗 獲得超7個贊
having可以理解為group by的where條件
不知道你這個ORM怎么寫,直接寫sql,在mysql下測試通過:
select a.*,count(a.product) as count from products a where date=(select max(date) from products b where b.user=a.user) group by user
- 1 回答
- 0 關注
- 252 瀏覽
添加回答
舉報
0/150
提交
取消