IP_Liulang 表结构如下:
id IP_address IP_datetime IP_url
1747 127.0.0.1 2009-11-18 15:10:02.467 http://localhost:1147/ttxs/admin/liulang.aspx
1748 127.0.0.1 2009-11-18 15:10:32.327 http://localhost:1147/ttxs/admin/IP_page.aspx
1749 127.0.0.1 2009-11-18 15:10:37.560 http://localhost:1147/ttxs/admin/liulang_GL.aspx
1750 127.0.0.1 2009-11-18 15:10:39.090 http://localhost:1147/ttxs/admin/liulang_GL.aspx
1751 219.143.136.9 2009-11-18 15:14:41.340 KL_room
1752 219.143.136.9 2009-11-18 15:15:36.217 http://60.195.249.197:8088/html/kl_room/top.aspx有三个sql语句:
第一是:
insert into IP_yestoday(IP_address,IP_num,IP_datetime ) select IP_address , count(1) , convert(varchar(10),IP_datetime,120)
from IP_Liulang group by IP_address ,convert(varchar(10),IP_datetime,120)
第二:
insert into IP_page(T_index,T_chat,T_task,T_KL_room,T_class_room,T_game,T_zsy,T_Course,T_friend,IP_datetime)
select
T_index=sum(case when IP_address='Index.aspx' then 1 else 0 end),
T_Chat=sum(case when IP_url='ChatRoom' then 1 else 0 end),
T_task=sum(case when IP_url='Task' then 1 else 0 end),
T_KL_room=sum(case when IP_url='KL_room' then 1 else 0 end),
T_class_room=sum(case when IP_url='VFClassroom' then 1 else 0 end),
T_game=sum(case when IP_url='Game' then 1 else 0 end),
T_zsy=sum(case when IP_url='Zsy' then 1 else 0 end),
T_Course=sum(case when IP_url='CoursewareManagement' then 1 else 0 end),
T_friend=sum(case when IP_url='friend' then 1 else 0 end),
convert(varchar(10),IP_datetime,120)
from IP_Liulang group by convert(varchar(10),IP_datetime,120)
第三:
insert into IP_page_fwl(IP_datetime,T_index,T_chat,T_task,T_KL_room,T_class_room,T_game,T_zsy,T_Course,T_friend)
select convert(varchar(10),IP_datetime,120) as fldDate,
T_index=sum(case when IP_url='Index.aspx' then ipcount else 0 end),
T_Chat=sum(case when IP_url='ChatRoom' then ipcount else 0 end),
T_task=sum(case when IP_url='Task' then ipcount else 0 end),
T_KL_room=sum(case when IP_url='KL_room' then ipcount else 0 end),
T_class_room=sum(case when IP_url='VFClassroom' then ipcount else 0 end),
T_game=sum(case when IP_url='Game' then ipcount else 0 end),
T_Zsy=sum(case when IP_url='Zsy' then ipcount else 0 end),
T_Course=sum(case when IP_url='CoursewareManagement' then ipcount else 0 end),
T_friend=sum(case when IP_url='friend' then ipcount else 0 end)
from (select count(IP_address) as ipcount,IP_datetime, IP_url from
(select distinct IP_address, convert(varchar(10), IP_datetime,120) as IP_datetime, IP_url from IP_Liulang)
as a group by IP_datetime, IP_url) as b group by convert(varchar(10),IP_datetime,120)
这三个语句都是从表IP_Liulang中查出数据插入到对应的三张表。
第一句对应的表是IP_yestoday:
id IP_address IP_num IP_datetime
254 127.0.0.1 15 5 2009-11-18
IP_num是 同一天相同IP_address出现的次数。第一句SQL语句可以实现这个,问题是,当IP_yestoday表中已经有这一天相同IP的数据量,再插入的话 怎么让原有的IP_num的值加上查出来的次数。
第二句对应的表是:IP_page
id IP_datetime T_index T_chat T_task T_KL_room T_class_room T_game T_zsy T_Course T_friend
49 2009-11-17 1 2 2 0 2 1 1 2 2 其中:T_index 对应:Index.aspx
其中:T_chat 对应:ChatRoom
其中:T_task 对应:Task
其中:T_KL_room 对应:KL_room
其中:T_class_room 对应:Index.aspx
其中:T_game 对应:Game
其中:T_zsy 对应:Zsy
其中:T_Course 对应:CoursewareManagement
其中:T_friend对应:friend
第二句SQL语句也可以实现插入,问题是,当被插入表IP_page已经有这天的数据,如何让对应的字段的值加上查出来的。第三局也是如此(对应表IP_page_fwl)
id IP_datetime T_index T_chat T_task T_KL_room T_class_room T_game T_zsy T_Course T_friend
49 2009-11-17 1 2 2 0 2 1 1 2 2
其中:T_index 对应:Index.aspx
其中:T_chat 对应:ChatRoom
其中:T_task 对应:Task
其中:T_KL_room 对应:KL_room
其中:T_class_room 对应:VFClassroom
其中:T_game 对应:Game
其中:T_zsy 对应:Zsy
其中:T_Course 对应:CoursewareManagement
其中:T_friend对应:friend 我的三个插入的问题都是 如果被插入的表中已经存在,如果在这基础上再加上查出来的。
谢谢高手,帮忙看看。最近发帖过多,分有点少,请见谅
id IP_address IP_datetime IP_url
1747 127.0.0.1 2009-11-18 15:10:02.467 http://localhost:1147/ttxs/admin/liulang.aspx
1748 127.0.0.1 2009-11-18 15:10:32.327 http://localhost:1147/ttxs/admin/IP_page.aspx
1749 127.0.0.1 2009-11-18 15:10:37.560 http://localhost:1147/ttxs/admin/liulang_GL.aspx
1750 127.0.0.1 2009-11-18 15:10:39.090 http://localhost:1147/ttxs/admin/liulang_GL.aspx
1751 219.143.136.9 2009-11-18 15:14:41.340 KL_room
1752 219.143.136.9 2009-11-18 15:15:36.217 http://60.195.249.197:8088/html/kl_room/top.aspx有三个sql语句:
第一是:
insert into IP_yestoday(IP_address,IP_num,IP_datetime ) select IP_address , count(1) , convert(varchar(10),IP_datetime,120)
from IP_Liulang group by IP_address ,convert(varchar(10),IP_datetime,120)
第二:
insert into IP_page(T_index,T_chat,T_task,T_KL_room,T_class_room,T_game,T_zsy,T_Course,T_friend,IP_datetime)
select
T_index=sum(case when IP_address='Index.aspx' then 1 else 0 end),
T_Chat=sum(case when IP_url='ChatRoom' then 1 else 0 end),
T_task=sum(case when IP_url='Task' then 1 else 0 end),
T_KL_room=sum(case when IP_url='KL_room' then 1 else 0 end),
T_class_room=sum(case when IP_url='VFClassroom' then 1 else 0 end),
T_game=sum(case when IP_url='Game' then 1 else 0 end),
T_zsy=sum(case when IP_url='Zsy' then 1 else 0 end),
T_Course=sum(case when IP_url='CoursewareManagement' then 1 else 0 end),
T_friend=sum(case when IP_url='friend' then 1 else 0 end),
convert(varchar(10),IP_datetime,120)
from IP_Liulang group by convert(varchar(10),IP_datetime,120)
第三:
insert into IP_page_fwl(IP_datetime,T_index,T_chat,T_task,T_KL_room,T_class_room,T_game,T_zsy,T_Course,T_friend)
select convert(varchar(10),IP_datetime,120) as fldDate,
T_index=sum(case when IP_url='Index.aspx' then ipcount else 0 end),
T_Chat=sum(case when IP_url='ChatRoom' then ipcount else 0 end),
T_task=sum(case when IP_url='Task' then ipcount else 0 end),
T_KL_room=sum(case when IP_url='KL_room' then ipcount else 0 end),
T_class_room=sum(case when IP_url='VFClassroom' then ipcount else 0 end),
T_game=sum(case when IP_url='Game' then ipcount else 0 end),
T_Zsy=sum(case when IP_url='Zsy' then ipcount else 0 end),
T_Course=sum(case when IP_url='CoursewareManagement' then ipcount else 0 end),
T_friend=sum(case when IP_url='friend' then ipcount else 0 end)
from (select count(IP_address) as ipcount,IP_datetime, IP_url from
(select distinct IP_address, convert(varchar(10), IP_datetime,120) as IP_datetime, IP_url from IP_Liulang)
as a group by IP_datetime, IP_url) as b group by convert(varchar(10),IP_datetime,120)
这三个语句都是从表IP_Liulang中查出数据插入到对应的三张表。
第一句对应的表是IP_yestoday:
id IP_address IP_num IP_datetime
254 127.0.0.1 15 5 2009-11-18
IP_num是 同一天相同IP_address出现的次数。第一句SQL语句可以实现这个,问题是,当IP_yestoday表中已经有这一天相同IP的数据量,再插入的话 怎么让原有的IP_num的值加上查出来的次数。
第二句对应的表是:IP_page
id IP_datetime T_index T_chat T_task T_KL_room T_class_room T_game T_zsy T_Course T_friend
49 2009-11-17 1 2 2 0 2 1 1 2 2 其中:T_index 对应:Index.aspx
其中:T_chat 对应:ChatRoom
其中:T_task 对应:Task
其中:T_KL_room 对应:KL_room
其中:T_class_room 对应:Index.aspx
其中:T_game 对应:Game
其中:T_zsy 对应:Zsy
其中:T_Course 对应:CoursewareManagement
其中:T_friend对应:friend
第二句SQL语句也可以实现插入,问题是,当被插入表IP_page已经有这天的数据,如何让对应的字段的值加上查出来的。第三局也是如此(对应表IP_page_fwl)
id IP_datetime T_index T_chat T_task T_KL_room T_class_room T_game T_zsy T_Course T_friend
49 2009-11-17 1 2 2 0 2 1 1 2 2
其中:T_index 对应:Index.aspx
其中:T_chat 对应:ChatRoom
其中:T_task 对应:Task
其中:T_KL_room 对应:KL_room
其中:T_class_room 对应:VFClassroom
其中:T_game 对应:Game
其中:T_zsy 对应:Zsy
其中:T_Course 对应:CoursewareManagement
其中:T_friend对应:friend 我的三个插入的问题都是 如果被插入的表中已经存在,如果在这基础上再加上查出来的。
谢谢高手,帮忙看看。最近发帖过多,分有点少,请见谅
---查重复
select * from 表 a
where exists (select 1 from 表 where a.字段=字段 and a.某一列<>某一列)
not exists(select 1 from tb where ...)
w 2006-02-02
w 2006-02-02
tb2
w 2006-02-02 2如果在插入的话,不是多一条记录,而是 tb2 的2变成3
意思是在插入的时候判断哪些已经存在 然后不插入已经存在的
insert into
表b
select
*
from
表a
where
exists (select 1 from 表 where a.字段=字段 and a.某一列<>某一列)
不是不插入已经存在的。如果已经存在的话,他出现的出现的次数会加起来。比如已经存在的num的值为3,查出来又有三条同一天相同Ip的记录,就把num的值变成是6
table1如下:
id ip datetime url
2289 219.143.139.241 2009-11-18 09:32:42.640 VFClassroom
2290 219.143.139.241 2009-11-18 09:32:56.043 KL_room
2291 219.143.139.241 2009-11-19 09:34:28.170 KL_room
2292 219.143.139.241 2009-11-19 09:57:20.750 Index.aspx
2293 219.143.139.241 2009-11-19 09:57:27.670 Index.aspx
2294 219.143.139.241 2009-11-19 09:57:32.327 Task
2295 219.143.139.241 2009-11-19 09:58:38.577 Index.aspx
2296 219.143.139.245 2009-11-19 09:58:51.793 Index.aspx
2297 219.143.139.245 2009-11-19 09:58:58.310 Task
2298 219.143.139.245 2009-11-19 09:59:22.170 Index.aspx
table2如何:
id ip num datetime
1 219.143.139.241 2 2009-11-18
2 219.143.139.241 4 2009-11-19
3 219.143.139.245 3 2009-11-19
table2的数据是来自table1得。插入到table2的规律是,如果同样的IP再同一天内出现了多次。就把出现的次数插入到table2中num字段。也就是说table2中数据得到意思是,某个IP在这一天出现的次数。我的问题是:当table2为空表时,从table1查出数据插入table2我会做。但是,当table2不是空表时,该怎么插入。
比如说table2中已经有2009-11-18这一天IP为219.143.139.241出现的次数为2,这时从table1中查出的数据还有这个IP在这天的记录,那么我们就给改变num的值,有一条就加1,有N条 就加N。
请问,当table2为空和不为空的两种情况下,用一个sql语句怎么实现。
(
id int,
Ip_Address varchar(18),
Ip_Datetime datetime,
Ip_Url varchar(256)
)
go
create table Ip_Yesterday
(
id int identity(1,1) primary key,
Ip_Address varchar(18),
Ip_num int,
Ip_Datetime datetime
)
go
insert into Ip_liulang
select 1747, '127.0.0.1', '2009-11-18 15:10:02.467', 'http://localhost:1147/ttxs/admin/liulang.aspx' union all
select 1748, '127.0.0.1', '2009-11-18 15:10:32.327', 'http://localhost:1147/ttxs/admin/IP_page.aspx' union all
select 1749, '127.0.0.1', '2009-11-18 15:10:37.560', 'http://localhost:1147/ttxs/admin/liulang_GL.aspx' union all
select 1750, '127.0.0.1', '2009-11-18 15:10:39.090', 'http://localhost:1147/ttxs/admin/liulang_GL.aspx' union all
select 1751, '219.143.136.9', '2009-11-18 15:14:41.340', 'KL_room' union all
select 1752, '219.143.136.9', '2009-11-18 15:15:36.217', 'http://60.195.249.197:8088/html/kl_room/top.aspx'
create proc My_Test
as
begin tran
insert into Ip_Yesterday
select Ip_Address,count(1),convert(varchar(10),IP_datetime,120) from IP_Liulang group by IP_address ,convert(varchar(10),IP_datetime,120)
select Ip_Address,Sum(Ip_num) as mycount,Ip_Datetime into #temp from Ip_Yesterday group by Ip_Address,Ip_Datetime
truncate table Ip_Yesterday
insert into Ip_Yesterday select * from #temp
if(@@error<>0)
rollback tran
else
begin
commit tran
end