比如排序之後我們想計算兩條相鄰記錄的時間差,因為mysql沒有視窗函式所以要麼模擬視窗函式要麼使用表自關聯,現有如下表資料:
單獨只有這些資訊使用自關聯沒有約束條件,這時候我們可以使用增加乙個序號的字段,然後使用序號條件關聯
表一:select a.*,(@i := @i + 1) as ord_num from t_punch_cade a,(select @i := 1) d order
by user_id,punch_time
表二:select a.*,(@j := @j + 1) as ord_num from t_punch_cade a,(select @j := 0) c order
by user_id,punch_time
兩張表其實是同一種表,只是乙個序號從1開始乙個序號從2開始:
有了這兩張表就可以計算相鄰兩張表時間欄位的差,得到的效果:
整個的sql語句:
select a.user_id,a.punch_time,timestampdiff(second,a.punch_time,b.punch_time) sub_seconds
from(
select a.*,(@i := @i + 1) as ord_num from t_punch_cade a,(select @i := 1) d order
by user_id,punch_time
) as a left
join (
select a.*,(@j := @j + 1) as ord_num from t_punch_cade a,(select @j := 0) c order
by user_id,punch_time
)as b on a.ord_num=b.ord_num and a.user_id=b.user_id
C 根據兩條線段 計算該兩條直線的交點
也可以理解為 三角形乙個頂點 連線 內部某乙個點,延長後和另一條邊的交點。計算兩條直線的交點 l1的點1座標 l1的點2座標 l2的點1座標 l2的點2座標 public static xyz getintersection xyz linefirststar,xyz linefirstend,xy...
MySQL 兩條記錄合併查詢成一條顯示
資料庫資料如下 張三,屬於a部門也屬於b部門,在表裡存的是兩條記錄,但是在頁面上我想要根據人來顯示,在一條記錄裡顯示張三屬於a部門和b部門,張三隻顯一條記錄,不顯示兩條。select t.id,t.code,t.name,max case t.type when a then t.dept name...
計算兩條直線的交點 C
ps 從其他地方看到的原始碼是有問題的。下面是修正後的 計算兩條直線的交點 l1的點1座標 l1的點2座標 l2的點1座標 l2的點2座標 public static pointf getintersection pointf linefirststar,pointf linefirstend,po...