![]() | 1 RemRain 2014-12-01 12:25:22 +08:00 SELECT * FROM xxx 然后在程序里面搞定 |
2 zts1993 2014-12-01 12:38:38 +08:00 |
![]() | 3 phoneli 2014-12-01 12:41:42 +08:00 感觉这样的逻辑业务。不应该在用sql占用数据库。你就select * 出来,然后用shell处理一下。 |
4 jianghu52 2014-12-01 12:57:35 +08:00 我好奇的是相邻两条这个要求怎么在sql里面体现出来 |
5 Automan 2014-12-01 12:59:39 +08:00 其实你只要DISTINCT就可以了。。因为只要是不同的时间,那肯定是大于一秒 |
![]() | 6 msg7086 2014-12-01 13:18:48 +08:00 数据库里哪来相邻…… |
![]() | 7 7654 2014-12-01 13:34:22 +08:00 这样解决吧,数据库应该只提供数据,不做分析 |
![]() | 8 beginor 2014-12-01 14:14:30 +08:00 对 MySql 不熟悉, 不过感觉用游标应该可以解决的; 如果是 SQL Server 的话, 除了游标之外, 可以使用 LEAD/LAG 函数来解决 http://msdn.microsoft.com/zh-cn/library/hh231256.aspx |
![]() | 9 loading 2014-12-01 14:19:35 +08:00 via Android 全选出来,order by id,process_time 然后在程序里判断就好了。 |
10 hcymk2 2014-12-01 14:23:41 +08:00 select a.id ,b.id ,a.card_num from (select id ,process_time,card_num, @num1 := @num1 + 1 as row_number from (SELECT @num1:=1) r1, you_table_name order by card_num,process_time ) a , (select id ,process_time,card_num, @num2 := @num2 + 1 as row_number from (SELECT @num2:=0) r2, you_table_name order by card_num,process_time) b where a.row_number=b.row_number and a.card_num=b.card_num and b.process_time-a.process_time>1; 献丑了。 |
![]() | 11 ixiaohei 2014-12-01 14:48:09 +08:00 |
![]() | 13 jucelin 2014-12-01 23:50:47 +08:00 via Android 可以考虑提前预处理数据,例如插入前就计算出与上一次的时间差算放进库里去。 |
![]() | 14 lu18887 2014-12-02 00:24:02 +08:00 via iPhone 楼上说程序处理的各位仁兄,当数据量足够大,计算要求足够复杂的时候数据库的SQL还是不错的选择,前提是要能写对。 |
![]() | 15 jinghli 2014-12-02 04:24:59 +08:00 这是典型的SQL window function.不算是太小白的问题。我简单写了个postgresql可用的。 SELECT card_num FROM ( SELECT lag(process_time) OVER (PARTITION BY card_num ORDER BY process_time) AS prev_process_time, card_num, process_time FROM your_table ) x WHERE prev_process_time is NULL --not sure whether you need this OR (process_time - prev_process_time > '1 second'::interval) |