MySQL数据库表:
create table t (
visitor_id int(11),
activity_type varchar(10),
date date
);
行:
insert into t (visitor_id, activity_type, date) values (1, 'hit', '2012-1-1');
insert into t (visitor_id, activity_type, date) values (1, 'event', '2012-1-2');
insert into t (visitor_id, activity_type, date) values (2, 'hit', '2012-1-2');
insert into t (visitor_id, activity_type, date) values (2, 'event', '2012-3-5');
insert into t (visitor_id, activity_type, date) values (2, 'hit', '2012-3-2');
insert into t (visitor_id, activity_type, date) values (1, 'hit', '2012-3-5');
insert into t (visitor_id, activity_type, date) values (1, 'hit', '2012-2-1');
我想编写一个查询来检索用户的数据转储,活动类型以及活动发生的顺序,如下所示:
visitor_id, activity_type, Position
1, hit, 1
1, event, 2
1, hit, 3
1, hit, 4
2, hit, 1
2, hit, 2
2, event, 3
到目前为止,我已经写了以下解决方案:
select visitor_id, activity_type, 'Position'
from t1
order by visitor_id, date
;
困难的部分是列位置。这应该表示该访客ID的行的顺序。有没有办法确定位置?