SQLite选择多组行

时间:2013-07-30 08:19:15

标签: sql sqlite limit where-in

我正在使用SQLite,尝试选择多组行。例如:

RowID  USER  Event
------------------
1      Sam   eventX  
2      Sam   eventY
3      Sam   eventA
4      John  E1
5      John  E5
6      Lisa  ev3
7      Lisa  ev4
8      Lisa  ev3

我想为每个USER选择一个条目,即具有最高rowid的条目。 结果集应如下所示:

RowID  USER  Event
------------------
3      Sam   eventA
5      John  E5
8      Lisa  ev3

请帮忙。

4 个答案:

答案 0 :(得分:2)

平均和精益:

SELECT * FROM Table1 WHERE RowID IN (
    SELECT MAX(RowID) FROM Table1 GROUP BY User
);

答案 1 :(得分:1)

<强> Please learn Aggregate function for SQL Lite

select max(RowID) as RowID,User,Event
from Table1
group by User
order by max(RowID)

<强> SQL FIDDLE

答案 2 :(得分:1)

select max(RowID),User,Event from Info group by User order by max(RowID)

信息表

enter image description here

<强>结果

enter image description here

答案 3 :(得分:0)

SELECT rowid, user, event FROM table WHERE rowid IN (SELECT MAX(rowid) FROM table GROUP BY user);