實現前後行相減的SQL該如何寫?有兩種方法!

2021-09-09 05:43:41 字數 3468 閱讀 9232

針對這類場景,sql語句改如何寫呢?

以統計前後行的訂單號是否連續為例,

表結構:

create table `t_test3` (

`id` int(11) not null auto_increment comment '自增id',

`order_id` int(11) default '0' comment '訂單號',

primary key (`id`),

key (`order_id`)

) engine=innodb default charset=utf8mb4 comment='';

插入測試資料:

insert into t_test3(order_id) values(1),(2),(3),(5),(6),(4),(2);

找order_id-1行,如果沒有,說明不存在order_id-1的order_id

mysql>select *, (select t1.order_id-order_id from t_test3 where order_id=t1.order_id-1 order by order_id desc limit 1) diff from t_test3 t1;

+----+----------+------+

| id | order_id | diff |

+----+----------+------+

| 1 | 1 | null |

| 2 | 2 | 1 |

| 7 | 2 | 1 |

| 3 | 3 | 1 |

| 4 | 5 | null |

| 6 | 5 | null |

| 5 | 6 | 1 |

+----+----------+------+

7 rows in set (0.00 sec)

不過,這種寫法有個缺點,不能再按diff過濾,會報錯。

mysql> select *, (select order_id-t1.order_id from t_test3 where order_id=t1.order_id-1 order by order_id desc limit 1) diff from t_test3 t1 where diff is null;

error 1054 (42s22): unknown column 'diff' in 'where clause'

order_idorder_id-1作為join條件

mysql> select * from t_test3 t1 left join (select * from t_test3  ) t2  on t1.order_id = t2.order_id-1 ;

+----+----------+------+----------+

| id | order_id | id | order_id |

+----+----------+------+----------+

| 1 | 1 | 2 | 2 |

| 1 | 1 | 7 | 2 |

| 2 | 2 | 3 | 3 |

| 7 | 2 | 3 | 3 |

| 4 | 5 | 5 | 6 |

| 6 | 5 | 5 | 6 |

| 3 | 3 | null | null |

| 5 | 6 | null | null |

+----+----------+------+----------+

8 rows in set (0.00 sec)

如果只查詢不存在的orderid,則直接加where條件:

mysql> select * from t_test3 t1 left join (select * from t_test3  ) t2  on t1.order_id = t2.order_id-1 where t2.order_id is null;

+----+----------+------+----------+

| id | order_id | id | order_id |

+----+----------+------+----------+

| 3 | 3 | null | null |

| 5 | 6 | null | null |

+----+----------+------+----------+

2 rows in set (0.00 sec)

輕鬆搞定!

一定要注意欄位的型別,如果是字串型別,又涉及到運算的則可能不會使用到索引。需要強轉回char比較

select u1.id,u1.detail_id,(select detail_id from t_order_detail where status=1 and create_time>='2019-01-09 16:00:00' 

and create_time<='2019-01-09 16:01:00'

-- 型別轉換

and convert((cast(detail_id as signed)-1),char(100))=u1.detail_id

) as diff

from t_order_detail u1 where status=1

and create_time>='2019-01-09 16:00:00'

and create_time<='2019-01-09 16:10:00';

select u1.id,u2.id,u1.detail_id,u2.detail_id  from t_order_detail u1 right join   

(select id,convert((cast(detail_id as signed)-1),char(100)) detail_id from t_order_detail where status=1

and create_time>='2019-01-09 16:00:00'

and create_time<='2019-01-09 16:01:00') u2

on u1.detail_id=u2.detail_id where u1.detail_id is null limit 100;

我的訂單前後端實現

功能描述 獲取當前使用者訂單列表 author cakin date 2021 1 3 param request http請求 return r 返回給前端的資料 apioperation value 獲取當前使用者訂單列表 介面 功能描述 獲取當前使用者訂單列表 author cakin dat...

用nginx實現前後端分離的專案的前後端訪問

現在幾乎都是前後端分析的專案,在開發和線上一般都會使用nginx做 來實現跨域和負債均衡。nginx安裝前面已經講過,現在寫寫如何配置,前端技術為vue。server location web server location web 前端 在路徑 var www html yanwei privat...

WebSocket 實現前後端通訊的筆記

引入依賴 在 spring 中要使用 websocket 功能,需要在pom中引入依賴 org.springframework.bootgroupid spring boot starter websocketartifactid dependency 配置類 增加乙個配置類,用於定義 websoc...