mysql數字典 mysql的數字的幾個經典問題

2021-10-18 22:29:46 字數 1166 閱讀 9795

之前發了乙個mysql時間函式的幾個經典問題,還有點沒消化好,今天是兩個關於數字的sql程式設計問題。乙個是數字輔助表,乙個是連續範圍問題。

a數字輔助表的問題是填充一張表,其中包含1-n的數

begin

declare s int unsigned default 1;

truncate table testtable;

while s<= num do

begin

insert into testtable select s;

set s=s+1;

end;

end while;

end換另一種寫法

begin

declare s int unsigned default 1;

truncate table testtable;

while s*2<= num do

begin

insert into testtable select id+s from testtable;

set s=s*2;

end;

end while;

end效率很高。。。執行時間為0.478ms

b 連續範圍問題是指一段數字為不連續的,如何能找到連續範圍

select id,@a :=@a+1 rn from testtable ,(select @a :=0) as a;

這條語句可以現實不連續的值及所在的rownum

select b.id,b.rn,b.id-b.rn diff from

select id,@a :=@a+1 rn from testtable ,(select @a :=0) as a

) as b

這個語句查出來了所有的差值,然後根據這個差值groupby下。

select

min(id) as min,max(id) as max

from (

select b.id,b.rn,b.id-b.rn diff from

select id,@a :=@a+1 rn from testtable ,(select @a :=0) as a

) as b

) as c group by diff;

總結一下,這兩個程式乙個是用來計算連續值,乙個是算出連續值,主要用了一些巧妙的演算法。

MySQL字典集排序 mysql字典

1 顯示資料庫列表 示例 mysql show databases 說明 其中字典庫是 information schema,其中常用字典表 information schema.schemata 資料庫中所有資料庫資訊 information schema.tables 存放資料庫中所有資料庫表資...

Mysql命令mysql 連線Mysql資料庫

mysql命令格式 mysql h主機位址 u使用者名稱 p使用者密碼 1 連線到本機上的mysql 首先開啟dos視窗,然後進入目錄mysql bin,再鍵入命令mysql u root p,回車後提示你輸密碼。注意使用者名稱前可以有空格也可以沒有空格,但是密碼前必須沒有空格,否則讓你重新輸入密碼...

mysql業務字典表 MySql常用字典表

1 顯示資料庫列表 mysql show databases 說明 其中字典庫是 information schema,其中常用字典表 information schema.schemata 資料庫中所有資料庫資訊 information schema.tables 存放資料庫中所有資料庫表資訊 i...