如何在数据库中建立动态的字段

jiedushi 2005-08-14 04:03:46
我有一个数据库ip是关于记录访问者的ip的有三个字段 id ip date
结构如下
id ip date
1 200.120.31.210 2005-7-14 7:10:22
2 100.69.200.21 2005-7-14 8:14:32
3 210.31.200.14 2005-7-15 9:00:14
......
我想做一个显示每天24小时的ip个数的asp程序 用表格的形式显示如下
0 1 2 3 4 5 6 7 8 9 。。。。23
2005-7-14
2005-7-15
2005-7-16
。。。。
2005-8-14
在用asp编程中用sql语句
insert into biao select count(ip) as ipshu from ip where year(date)=year(getdate()) and month(date)=month(getdate()) and day(date)=day(getdate()) group by hour(getdate())
将当天的每个小时的ip个数插入到表中但是要是从数据库里面读出数据库内容就比较困难了
因此我想要是能够动态建立当天的日期的字段应该就比较容易读取了,不知道怎么实现,请教高手多多指教,如果这个方法不可以的话,其他的方法也可以指出,多谢了
...全文
93 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
filebat 2005-08-14
  • 打赏
  • 举报
回复
--测试数据
create table ipinfo(id int, ip varchar(20), date datetime)
insert ipinfo select 1,'200.120.31.210','2005-7-14 7:10:22'
union all select 2,'100.69.200.21' ,'2005-7-14 8:14:32'
union all select 3,'210.31.200.14' ,'2005-7-15 9:00:14'
--查询
declare @str varchar(8000), @i int
select @i=0,@str='select 日期=convert(varchar(10), date, 120)'
while @i<24
begin
set @str=@str+'
,['+convert(varchar(2), @i)+']=sum(case
when datepart(hh, date)>='+convert(varchar(2), @i)+
' and datepart(hh, date)<'+convert(varchar(2), @i+1)+
' then 1 else 0 end)'
set @i=@i+1
end
set @str=@str+'
from ipinfo
group by convert(varchar(10), date, 120)'
--print @str
exec(@str)
--清除
drop table ipinfo
filebat 2005-08-14
  • 打赏
  • 举报
回复
用交叉数据报表就可以比较简单的实现.
不过,你的结果集中有二十五列。
如果一列一列的写出来的话,那代码算起来就比较零乱。
你可以通过动态sql语句来实现。
这样看起来舒服一些。

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧