如何获取此sqlite查询的结果?
如何获取此sqlite查询的结果?,sqlite,Sqlite,我想要得到结果: 我需要统计表中的最后一项来记录连续发生的次数 columnname字符时间 col1 A 3 col2 B 4 col3 C 2 col4 D 5 您可以使用多重查询: SELECT 'col1' as `cols`,col1 as `character`,count(col1) as `nbr` FROM `ets` where id >
我想要得到结果:
我需要统计表中的最后一项来记录连续发生的次数 columnname字符时间
col1 A 3
col2 B 4
col3 C 2
col4 D 5
您可以使用多重查询:
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
到目前为止你试过什么?请提供一个定制的玩具数据库。通过使用
.dump
功能或手动列出几行创建表格…
和插入…
来执行此操作。这样做会比发布一张不完全代表数据库的文本图片得到更多的关注和帮助。Acol1、BCol2、CCol3、DCol4背后的逻辑是什么。顺序的凭什么?在每一列中,是否只有尽可能多的不同值?请解释更多关于这个背景和目标的信息。我怀疑,如果没有一些逻辑解释,那些碰巧为给定样本数据创建了所需输出的答案有很大的机会实现您实际想要的结果。例如,如果还有一些“F”,那么所需输出会是什么?还是不可能?如果不可能,为什么?或者,如果有随机A、B、C、D,col5的期望输出是什么?或者,如果这是不可能的,那么为什么呢?列名的数据是一个列更改,字符的数据是最后一条记录,数据的数量是不能生成输出的连续发生数,该输出也有类似于col1b1
或col4b2
的行?你测试过这个吗?或者你认为这是OP acutally想要的作为输出的一部分吗?啊,我看到信息更新了是的,我更新了我的解决方案,非常感谢你!你的计划可以实现。有更有效的解决方案吗?欢迎兄弟,我想还有另一个解决方案是程序,但我更喜欢使用查询
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