用SQL語句實現的網咖計費

2021-04-12 12:05:26 字數 3526 閱讀 1428

以下是csdn中的問題:

首先在sql裡用getdate()得到當前登陸時間,然後以預存金額與所定義的時段收費(時段收費是在另一張表中,有二十四個字段,從零到二十三,裡面記錄了每個小時的收費標準)進行減運算,逐一相減,比如20塊,現在時間為晚上八點,那麼就是20-八點收費標準-九點收費標準以此類推,直到小於或等於1,然後用餘數與下乙個時段收費標準進行相除,得出所能上網的時長,精確到分,然後與當前登陸時間進行相加,然後得出預計下機時間。 

下面是語句:

create table fee(

f0 decimal(18,2),

f1 decimal(18,2),

f2 decimal(18,2),

f3 decimal(18,2),

f4 decimal(18,2),

f5 decimal(18,2),

f6 decimal(18,2),

f7 decimal(18,2),

f8 decimal(18,2),

f9 decimal(18,2),

f10 decimal(18,2),

f11 decimal(18,2),

f12 decimal(18,2),

f13 decimal(18,2),

f14 decimal(18,2),

f15 decimal(18,2),

f16 decimal(18,2),

f17 decimal(18,2),

f18 decimal(18,2),

f19 decimal(18,2),

f20 decimal(18,2),

f21 decimal(18,2),

f22 decimal(18,2),

f23 decimal(18,2)

)insert into fee values(1.0,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,1.0,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,1.0,1.1,1.2,1.3)

select * from fee

create table u(

m decimal(18,2),

startdt datetime,

enddt datetime

)insert into u values(20,getdate(),getdate())

select * from u

update u set m=10

declare @money as decimal(18,2),@fee as decimal(18,2),@hour as int,@day as datetime,@minute as int,@tempdt as datetime

select @money=m,@day=startdt from u--取出錢數和開始時間

set @hour=datepart(hh,@day)--取得當前小時

set @tempdt=@day

set @minute=datediff(mi,@day,cast(convert(varchar(11),@day,120)+cast(@hour+1 as varchar(10))+':00:00' as datetime))--得出差多長時間到下小時

--取出本小時費用

select @fee=case @hour

when 0 then f0

when 1 then f1

when 2 then f2

when 3 then f3

when 4 then f4

when 5 then f5

when 6 then f6

when 7 then f7

when 8 then f8

when 9 then f9

when 10 then f10

when 11 then f11

when 12 then f12

when 13 then f13

when 14 then f14

when 15 then f15

when 16 then f16

when 17 then f17

when 18 then f18

when 19 then f19

when 20 then f20

when 21 then f21

when 22 then f22

when 23 then f23 end

from fee

if(@money<=@minute*@fee/60)--如果錢數不夠付本小時就計算出可以上多少分鐘

begin

update u set enddt=dateadd(mi,@money/(@fee/60),startdt)--更新表

endelse

begin

set @money=@money-@minute*@fee/60--計算出到本小時結束還剩多少錢

set @tempdt=dateadd(mi,@minute,@tempdt)--累加時間到臨時變數中

set @hour=@hour+1

while(@money>0)

begin

--取出費用

select @fee=case @hour

when 0 then f0

when 1 then f1

when 2 then f2

when 3 then f3

when 4 then f4

when 5 then f5

when 6 then f6

when 7 then f7

when 8 then f8

when 9 then f9

when 10 then f10

when 11 then f11

when 12 then f12

when 13 then f13

when 14 then f14

when 15 then f15

when 16 then f16

when 17 then f17

when 18 then f18

when 19 then f19

when 20 then f20

when 21 then f21

when 22 then f22

when 23 then f23 end

from fee

--如果還夠付下一小時費用時就把臨時變數加1小時,錢數相應減少

if(@money>=@fee)

begin

set @tempdt=dateadd(hh,1,@tempdt)

set @money=@money-@fee

endelse

begin

update u set enddt=dateadd(mi,@money/(@fee/60),@tempdt)--不夠一小時就計算出分鐘,更新表中資料

set @money=0

endset @hour=@hour+1

endend

用sql語句彙總 實現語句的合併

待驗收入庫單 create table t cw warehousing entry bosid number not null,wareid varchar2 50 入庫單編號 stocktime varchar2 50 採購時間 empid varchar2 50 登記人 cw addr var...

用SQL語句實現遞迴演算法

本文通過乙個bom表的例子,分別介紹在sql server2000和sql server2005中如何編寫遞迴演算法。一 建立測試資料 createtablebillofmaterial productno nvarchar 15 父元件編號 productname nvarchar 50 子元件名...

打造網咖計費安全的「四重門」

編外 本文是廣告軟文 隨著奶粉事件在全國掀起軒然大波,安全 幾乎動搖人們心裡固守的最後一道防線。食品安全 生產安全 網路安全 提起來就讓人頭痛。而涉及到9918萬網咖網民的網咖計費安全更不可忽視。網咖計費安全牽涉範圍廣,關係到網咖 網民的切身利益。由於資料的敏感性,與之相關的資訊保安問題在設計規劃初...