CREATE TABLE [test] ([id] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,[col1] TEXT NULL,[col2] TEXT NULL,[col3] TEXT NULL,[col4] TEXT NULL);
INSERT INTO test (col1,col2,col3,col4)VALUES ('A','B','C','D');
INSERT INTO test (col1,col2,col3,col4)VALUES ('D','B','D','D');
INSERT INTO test (col1,col2,col3,col4)VALUES ('A','B','A','D');
INSERT INTO test (col1,col2,col3,col4)VALUES ('B','C','B','B');
INSERT INTO test (col1,col2,col3,col4)VALUES ('A','B','D','D');
INSERT INTO test (col1,col2,col3,col4)VALUES ('C','B','C','D');
INSERT INTO test (col1,col2,col3,col4)VALUES ('A','B','D','D');
INSERT INTO test (col1,col2,col3,col4)VALUES ('A','B','C','D');
INSERT INTO test (col1,col2,col3,col4)VALUES ('A','B','C','D');
我想得到结果:
我需要统计表中的最后一项来记录连续出现的次数
columnname字符时间
col1 A 3
col2 B 4
col3 C 2
col4 D 5
答案 0 :(得分:-1)
您可以使用多重查询:
SELECT 'col1' as `cols`,col1 as `character`,count(col1) as `nbr` FROM `ets` where id > (SELECT id FROM `ets` where col1 != (SELECT col1 FROM `ets` where id = (select count(col1) from `ets` )) order by id desc limit 1) group by col1
union all
SELECT 'col2' as `cols`,col2 as `character`,count(col2) as `nbr` FROM `ets` where id > (SELECT id FROM `ets` where col2 != (SELECT col2 FROM `ets` where id = (select count(col2) from `ets` )) order by id desc limit 1) group by col2
union all
SELECT 'col3' as `cols`,col3 as `character`,count(col3) as `nbr` FROM `ets` where id > (SELECT id FROM `ets` where col3 != (SELECT col3 FROM `ets` where id = (select count(col3) from `ets` )) order by id desc limit 1) group by col3
union all
SELECT 'col4' as `cols`,col4 as `character`,count(col4) as `nbr` FROM `ets` where id > (SELECT id FROM `ets` where col4 != (SELECT col4 FROM `ets` where id = (select count(col4) from `ets` )) order by id desc limit 1) group by col4