1、count函数里写表达式
#无效写法,这样写不会判断表达式(ischecked=0),会全部列出来 SELECT cardid FROM search_detail GROUP BY cardid HAVING COUNT(ischecked=0 )>1; #正确写法,这样如果不满足表达式(ischecked=0),就会置为空,就不计算在内 SELECT cardid FROM search_detail GROUP BY cardid HAVING COUNT(ischecked=0 OR NULL)>1;
2、计算时间差值
#巧妙的把同一个cardid的记录分别标了号,这样相邻两条记录就会有同一个标号,减一下就是时间间隔,而首尾的两条记录各对应一条null,不参与计算。 select A.id,B.id idb,A.cardid,A.sintime,B.sintime sintimeb,TIMESTAMPDIFF(DAY,A.sintime,B.sintime) sub_days from( select a.*,(@i := @i + 1) as ord_num from loan_16 a,(select @i := 1) d order by cardid,sintime ) as A LEFT JOIN ( select a.*,(@j := @j + 1) as ord_num from loan_16 a,(select @j := 0) c order by cardid,sintime )as B on A.ord_num=B.ord_num and A.cardid=B.cardid;