sqlserver---获取前一天的数据

--使用 CONVERT函数格式化时间,

--dateadd函数 前x天,后x天的方法

--查询后,直接插入数据

 insert into  [guagua_ktv].[dbo].[t_song_total] (song_name, room_id, totalnum, create_time)

  (
select song_name, room_id, totalnum, CONVERT(varchar(100), dateadd(day, -3, create_time), 20)
  from  [guagua_ktv].[dbo].[t_song_total] t 
  where CONVERT(varchar(100),  t.create_time, 23) = '2013-08-12'

  );



   insert into  [guagua_ktv].[dbo].[t_user_total] (user_id, room_id, goldmike_num, create_time)
  (
select user_id, room_id, goldmike_num, CONVERT(varchar(100),dateadd(day, -3, create_time), 20)
  --select singer_uid, room_id, totalnum, create_time, goldmike
  from  [guagua_ktv].[dbo].[t_user_total] t 
  where CONVERT(varchar(100),  t.create_time, 23) = '2013-08-12'
  );



  insert into [guagua_ktv].[dbo].[t_singer_total] (singer_uid, room_id, totalnum, create_time, goldmike)
  (
select singer_uid, room_id, totalnum, CONVERT(varchar(100),dateadd(day, -3, create_time), 20), goldmike
  --select singer_uid, room_id, totalnum, create_time, goldmike
  from [guagua_ktv].[dbo].[t_singer_total] t 
  where CONVERT(varchar(100),  t.create_time, 23) = '2013-08-12'
  );

你可能感兴趣的:(数据库)