數(shù)據(jù)結(jié)構(gòu)是這樣的date | not uniquser | not uniqproduct | uniq我要做這樣一個(gè)查詢?nèi)〕鏊杏脩?,最近發(fā)出的一個(gè)產(chǎn)品,并且這個(gè)列表是按照date排序的。比如數(shù)據(jù)如下--
date | user | product20120901 | John | A20120903 | Tom | B20120802 | John | C20120906 | John | D20120821 | Tom | E20120902 | Tom | F20120825 | Tom | G我希望得到數(shù)據(jù)--
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 回答

鴻蒙傳說(shuō)
TA貢獻(xiàn)1865條經(jīng)驗(yàn) 獲得超7個(gè)贊
having可以理解為group by的where條件
不知道你這個(gè)ORM怎么寫,直接寫sql,在mysql下測(cè)試通過(guò):
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 關(guān)注
- 262 瀏覽
添加回答
舉報(bào)
0/150
提交
取消