close
LV超3A名牌購物網

SQL序列找出断号
href="/question/#'" class='liveword' title='方法'>方法一:

select (select max(id)+1 from 你的資料表 where id<a.id) as beginId,(id-1) as endId
from 你的資料表 a
where
a.id>(select max(id)+1 from 你的資料表 where id<a.id)




方法二:

select beginId,(select min(id)-1 from 你的資料表 where id > beginId) as endId
from (
select id+1 as beginId from 你的資料表 where id+1 not in (select id from 你的資料表) and id < (select max(id) from 你的資料表)
) as t



**********************************************************
但上面的缺點是:

如果您是一開始的 id編號缺乏,就找不出來。
例如從一號開始編號。

但您把 1 / 2 / 3三個編號刪除了,他是呈現不出來的

只能從 4號開始,把遺失的、已刪除的 id流水號給呈現出來。


**********************************************
ASP.NET 專題實務與教學網站

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 方志遠 的頭像
    方志遠

    Lv,Gucci,Lv包包,Lv錢夾,Lv手錶,Lv目錄,Gucci公仔包,Lv購物包,Lv水桶包,Lv批發,愛美仕名牌購物

    方志遠 發表在 痞客邦 留言(0) 人氣()