获取具有id的行列表,按其他ID分组

时间:2016-06-30 09:07:49

标签: android sqlite

我在Android中使用SQLite(minSdkVersion:15)。我有这样的表: Table

我试图以trainingId作为参数获取最后插入行的列表。例如,我想获取带有trainingId = 1的最后插入行的列表,结果应为:

13---5---1 14---5---1 15---5---1

trainingId = 2,然后:

10---4---2 11---4---2 12---4---2

我试过这样的事情:

SELECT * FROM table_name JOIN (SELECT MAX(trainingDoneId) AS trainingDoneId FROM table_name GROUP BY treningId) USING (trainingDoneId)

SELECT * FROM table_name WHERE trainingDoneId IN (SELECT MAX(trainingDoneId) FROM table_name GROUP BY 1)

但在这两种情况下我都会收到此错误:

android.database.sqlite.SQLiteException: aggregate functions are not allowed in the GROUP BY clause (code 1)

有什么建议吗?

2 个答案:

答案 0 :(得分:2)

请点击以下查询它会帮助你,

  SELECT * FROM table_name where trainingId = 1 AND trainingDoneId in (select max(trainingDoneId) from table_name where trainingId = 1)

答案 1 :(得分:0)

SELECT id, MAX(trainingDoneId), trainingId FROM table_name WHERE trainingId = 1 ORDERBY id

尝试使用此解决方案