一、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;
二、計算時間差值spa
#巧妙的把同一個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;