Mysql實戰之求出缺失範圍

2021-08-28 08:33:10 字數 2812 閱讀 2295

1.需求

求出缺失範圍

2.示例

根據表中某個變化字段,求出變化欄位的缺失範圍。如下給出乙個例子:

下面是乙個表x,其中的資料如下:

mysql> select * from x;

+------+

| a |

+------+

| 3 |

| 4 |

| 5 |

| 6 |

| 100 |

| 101 |

| 102 |

| 105 |

| 106 |

| 107 |

+------+

10 rows in set (0.03 sec)

其中的缺失範圍就是:7-99,103-104。

3.**一

select a

from x as x1

where not exists

( select *

from x as x2

where x1.a + 1 = x2.a

);

執行結果如下:

+------+

| a |

+------+

| 6 |

| 102 |

| 107 |

+------+

3 rows in set (0.00 sec)

select a+1 as start_range

from x as x1

where not exists

( select *

from x as x2

where x1.a + 1 = x2.a

)and a< (select max(a) from x);

執行結果如下:

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

| start_range |

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

| 7 |

| 103 |

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

2 rows in set (0.00 sec)

select 

a+1 as start_range

,( select

min(a) -1

from x as x3

where x3.a > x1.a

) as end_range

from x as x1

where not exists

( select a

from x as x2

where x1.a + 1 = x2.a

)and a< (select max(a) from x);

執行結果如下:

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

| start_range | end_range |

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

| 7 | 99 |

| 103 | 104 |

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

2 rows in set (0.00 sec)

**二
select a as cur,

( select

min(a)

from x as x1

where x1.a > x2.a

) as next

from x as x2;

執行結果如下:

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

| cur | next |

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

| 3 | 4 |

| 4 | 5 |

| 5 | 6 |

| 6 | 100 |

| 100 | 101 |

| 101 | 102 |

| 102 | 105 |

| 105 | 106 |

| 106 | 107 |

| 107 | null |

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

10 rows in set (0.00 sec)

select

cur+1 as start_range,

next-1 as end_range

from

( select

a as cur,

(select min(a) from x as x2

where x2.a > x1.a)as next

from x as x1

)as x3

where next - cur > 1

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

| start_range | end_range |

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

| 7 | 99 |

| 103 | 104 |

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

2 rows in set (0.01 sec)

MySQL實戰之鎖

來自極客時間,林曉斌 丁奇 的mysql實戰45講 全域性鎖就是對整個資料庫例項加鎖。mysql 提供了乙個加全域性讀鎖的方法,命令是flush tables with read lock ftwrl 當你需要讓整個庫處於唯讀狀態的時候,可以使用這個命令,之後其他執行緒的以下語句會被阻塞 應用場景 ...

資料探勘實戰 資料預處理之缺失值處理

kaggle的titanic 比賽不少題解有標準的處理流程,這裡參考 kaggle titanic 生存 詳細流程 梳理 嘗試提取常用的缺失值處理方法 這裡還是借助google colab 來學習 讀取資料 import pandas as pd data pd.read csv data trai...

Mysql職業 MySQL企業實戰之DBA工作簡介

敬請期待 免費剩餘 時長 回放正在直播中 直播結束 免費敬請期待 id 1621 isdefault 1 learnmode freemode ismember status published currenttaskid ishideunpublish 0 i18nchaptername 章 i1...