A表為目標手機號
B表為資料表,手機號,交易金額,交易完成時間。
想通過以下陳述句查詢各個交易時間并計算各個時間之間的時間差。
SELECT
A.`手機號`,
B.`交易完成時間` AS 時間
FROM
`手機號` AS A ,
`收銀` AS B
where A.`手機號`=B.`手機號碼`
GROUP BY A.`手機號`, B.`交易完成時間`
難點:我是小白,不知道怎么計算多次交易完成時間的時間差。
一個手機號有多次購物時間,有的也只有一次,所以不知道咋辦了。
https://bbs.csdn.net/topics/320082158 這個帖子我也看了下,不知道怎么寫的,也模仿不成功。
理想的結果是:
手機號 交易完成時間 時間差
555 20200606 0
555 20200607 1
555 20200610 3
666 20200611 0
大概就這樣。。求各位大神幫忙看下,我請大佬們喝闊落。
uj5u.com熱心網友回復:

這個是我上述陳述句資料查詢的結果。。
uj5u.com熱心網友回復:
with cs as(
select 手機號,日期,row_number()over(order by 手機號) as id from table
)
select a.手機號,a.日期,case when b.id is null then 0 else a.日期- b.日期 end ce
from cs a left join cs b on a.id = b.id+1 and a.手機號 = b.手機號
uj5u.com熱心網友回復:
老大,怎么查詢不了。
uj5u.com熱心網友回復:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'cs AS ( SELECT 手機號,日期, row_number ( ) over ( ORDER BY 手機號 ) AS i' at line 1
提示這個。。
uj5u.com熱心網友回復:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'cs AS ( SELECT 手機號,日期, row_number ( ) over ( ORDER BY 手機號 ) AS i' at line 1提示的這個有大佬知道意思嘛。。
uj5u.com熱心網友回復:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'cs AS ( SELECT 手機號,日期, row_number ( ) over ( ORDER BY 手機號 ) AS i' at line 1
提示的這個有大佬知道意思嘛。。
應該跟你的mysql版本有關系,你版本可能不支持 with as 語法
你改寫一下,使用子查詢就可以了
select a.手機號,a.日期,case when b.id is null then 0 else a.日期- b.日期 end ce
from
(
select 手機號,日期,row_number()over(order by 手機號) as id from table
) as a
left join (
select 手機號,日期,row_number()over(order by 手機號) as id from table
) as b on a.id = b.id+1 and a.手機號 = b.手機號
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/13353.html
標籤:MySQL
