求一個批量修改記錄的sql

BILLSSJONE 2010-04-30 03:42:05
修改條件是:將不同用戶的最後一條時間記錄為16:20至17:00的時間統一修改為17:00
如:
1.每個用戶都要一個添加記錄的時間date1,每個用戶一天會添加多條記錄,
2.將每個用戶每天添加時間在16:20至17:00的最後一條記錄的時間改為17:00(一個用戶在16:20至17:00這個時間段可能有多條記錄,只改最後一條),最後一條記錄是16:20之前的就不做修改。
不知說明白了沒有,怎麼寫SQL?
...全文
236 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
dawugui 2010-05-01
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 xys_777 的回复:]
引用 15 楼 billssjone 的回复:
我試了dawugui的方法1,可以,先用先,然後再試試上面其它的方法,謝謝

那个会把重复时间的都变成17:00
[/Quote]
如果他说的是7,8,11楼的就没有问题.
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 fredrickhu 的回复:]

SQL code
update
tb
set
date1 = '17:00'
from
tb t
where
date1
between
'16:20' and '17:00'
and
not exists(select 1 from tb where 用户ID = t.用户ID and date1>t.date1 and date1 between '16:20' ……
[/Quote]
严重同意。
BILLSSJONE 2010-04-30
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 zyj_604 的回复:]
引用 2 楼 dawugui 的回复:
SQL code
update tb set date1 = '17:00' from tb t where date1 between '16:20' and '17:00' and date1 =
(select max(date1) from tb where date1 between '16:20' and '17:00' and 用户ID ……
[/Quote]
时间格式是smalldatetime的,要是这样set date1 = '17:00'那这个年月日不是没了?
BILLSSJONE 2010-04-30
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 xys_777 的回复:]
引用 15 楼 billssjone 的回复:
我試了dawugui的方法1,可以,先用先,然後再試試上面其它的方法,謝謝

那个会把重复时间的都变成17:00
[/Quote]

是吗?那么哪一条sql比较好呢?我现在不能测试。
永生天地 2010-04-30
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 billssjone 的回复:]
我試了dawugui的方法1,可以,先用先,然後再試試上面其它的方法,謝謝
[/Quote]
那个会把重复时间的都变成17:00
BILLSSJONE 2010-04-30
  • 打赏
  • 举报
回复
我試了dawugui的方法1,可以,先用先,然後再試試上面其它的方法,謝謝
zyj_604 2010-04-30
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 dawugui 的回复:]
SQL code
update tb set date1 = '17:00' from tb t where date1 between '16:20' and '17:00' and date1 =
(select max(date1) from tb where date1 between '16:20' and '17:00' and 用户ID = t.用户ID )
[/Quote]

up
永生天地 2010-04-30
  • 打赏
  • 举报
回复
现在时间刚好做测试


create table tab ([key] int identity(1,1),date1 smalldatetime,username varchar(10))

insert tab
select getdate(),'111' union all
select getdate(),'222' union all
select getdate(),'333'

select * from tab
--数据刚好
/*
key date1 username
----------- ------------------------------------------------------ ----------
15 2010-04-30 16:16:00 333
16 2010-04-30 16:26:00 111
17 2010-04-30 16:26:00 222
18 2010-04-30 16:26:00 333
19 2010-04-30 16:26:00 111
20 2010-04-30 16:26:00 222
21 2010-04-30 16:26:00 111
22 2010-04-30 16:27:00 111
23 2010-04-30 16:27:00 111
24 2010-04-30 16:27:00 111
25 2010-04-30 16:27:00 222
26 2010-04-30 16:27:00 111
27 2010-04-30 16:27:00 222
28 2010-04-30 16:27:00 333

(所影响的行数为 28 行)
*/

-- 先用select看看结果是否正确
select * from tab t1
where [key] = (
select top 1 t2.[key] from tab t2
where t2.date1 = (
select max(t3.date1) from tab t3
where t2.username=t3.username and convert(datetime,t3.date1) between left(getdate(),10)+' 16:20:00' and left(getdate(),10)+' 17:00:00'
) and t1.username=t2.username
)
/*
key date1 username
----------- ------------------------------------------------------ ----------
22 2010-04-30 16:27:00 111
25 2010-04-30 16:27:00 222
28 2010-04-30 16:27:00 333

(所影响的行数为 3 行)


*/

--再换update

update t1
set t1.date1=left(t1.date1,10)+' 17:00:00'
from tab t1
where [key] = (
select top 1 t2.[key] from tab t2
where t2.date1 = (
select max(t3.date1) from tab t3
where t2.username=t3.username and convert(datetime,t3.date1) between left(getdate(),10)+' 16:20:00' and left(getdate(),10)+' 17:00:00'
) and t1.username=t2.username
)

/*(所影响的行数为 3 行)
*/
select * from tab

/*
20 2010-04-30 16:26:00 222
21 2010-04-30 16:26:00 111
22 2010-04-30 17:00:00 111
23 2010-04-30 16:27:00 111
24 2010-04-30 16:27:00 111
25 2010-04-30 17:00:00 222
26 2010-04-30 16:27:00 111
27 2010-04-30 16:27:00 222
28 2010-04-30 17:00:00 333

(所影响的行数为 28 行)*/
BILLSSJONE 2010-04-30
  • 打赏
  • 举报
回复
讓我試試先
dawugui 2010-04-30
  • 打赏
  • 举报
回复
create table tb(date1 datetime,username int)
insert into tb values('2010-04-30 16:21:00' , 1)
insert into tb values('2010-04-30 16:11:00' , 1)
insert into tb values('2010-04-30 16:22:00' , 1)
insert into tb values('2010-04-30 16:21:00' , 1)
insert into tb values('2010-04-30 16:35:00' , 1)
insert into tb values('2010-04-30 16:45:00' , 1)
insert into tb values('2010-04-30 16:21:00' , 2)
insert into tb values('2010-04-30 16:11:00' , 2)
insert into tb values('2010-04-30 16:22:00' , 2)
insert into tb values('2010-04-30 16:21:00' , 2)
insert into tb values('2010-04-30 16:35:00' , 2)
insert into tb values('2010-04-30 16:45:00' , 2)
insert into tb values('2010-04-29 16:21:00' , 1)
insert into tb values('2010-04-29 16:11:00' , 1)
insert into tb values('2010-04-29 16:22:00' , 1)
insert into tb values('2010-04-29 16:21:00' , 1)
insert into tb values('2010-04-29 16:35:00' , 1)
insert into tb values('2010-04-29 16:45:00' , 1)
go

--方法一
update tb set date1 = convert(varchar(10),date1,120) + ' 17:00:00' from tb t
where convert(varchar(5),date1,108) between '16:20' and '17:00' and date1 =
(select max(date1) from tb where convert(varchar(5),date1,108) between '16:20' and '17:00' and username = t.username and convert(varchar(10),date1,120) = convert(varchar(10),t.date1,120))

--方法二
update tb set date1 = convert(varchar(10),date1,120) + ' 17:00:00' from tb t
where convert(varchar(5),date1,108) between '16:20' and '17:00' and not exists
(select 1 from tb where convert(varchar(5),date1,108) between '16:20' and '17:00' and username = t.username and convert(varchar(10),date1,120) = convert(varchar(10),t.date1,120) and date1 > t.date1)

select * from tb

drop table tb

/*
date1 username
------------------------------------------------------ -----------
2010-04-30 16:21:00.000 1
2010-04-30 16:11:00.000 1
2010-04-30 16:22:00.000 1
2010-04-30 16:21:00.000 1
2010-04-30 16:35:00.000 1
2010-04-30 17:00:00.000 1
2010-04-30 16:21:00.000 2
2010-04-30 16:11:00.000 2
2010-04-30 16:22:00.000 2
2010-04-30 16:21:00.000 2
2010-04-30 16:35:00.000 2
2010-04-30 17:00:00.000 2
2010-04-29 16:21:00.000 1
2010-04-29 16:11:00.000 1
2010-04-29 16:22:00.000 1
2010-04-29 16:21:00.000 1
2010-04-29 16:35:00.000 1
2010-04-29 17:00:00.000 1

(所影响的行数为 18 行)
*/
dawugui 2010-04-30
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 billssjone 的回复:]
只修改第天後一條16:20-17:00范圍內的記錄,其它倒數第二條以前的記錄就算在16:20-17:00范圍內也不做修改
[/Quote]
只修改每天後一條16:20-17:00范圍內的記錄,其它倒數第二條以前的記錄就算在16:20-17:00范圍內也不做修改?

那我上面的两条语句都行.
BILLSSJONE 2010-04-30
  • 打赏
  • 举报
回复
只修改第天後一條16:20-17:00范圍內的記錄,其它倒數第二條以前的記錄就算在16:20-17:00范圍內也不做修改
dawugui 2010-04-30
  • 打赏
  • 举报
回复
update tb set date1 = convert(varchar(10),date1,120) + ' 17:00:00' from tb t 
where convert(varchar(5),date1,108) between '16:20' and '17:00' and date1 =
(select max(date1) from tb where convert(varchar(5),date1,108) between '16:20' and '17:00' and username = t.username and convert(varchar(10),date1,120) = convert(varchar(10),t.date1,120))



update tb set date1 = convert(varchar(10),date1,120) + ' 17:00:00' from tb t 
where convert(varchar(5),date1,108) between '16:20' and '17:00' and not exists
(select 1 from tb where convert(varchar(5),date1,108) between '16:20' and '17:00' and username = t.username and convert(varchar(10),date1,120) = convert(varchar(10),t.date1,120) and date1 > t.date1)
dawugui 2010-04-30
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 billssjone 的回复:]
數據庫是sql server2005,假如就3個這段:
自動編號:key
添加時間:date1
用戶名:username
[/Quote]
update tb set date1 = convert(varchar(10),date1,120) + ' 17:00:00' from tb t 
where convert(varchar(5),date1,108) between '16:20' and '17:00' and date1 =
(select max(date1) from tb where convert(varchar(5),date1,108) between '16:20' and '17:00' and username = t.username and convert(varchar(10),date1,120) = convert(varchar(10),t.date1,120))
chuifengde 2010-04-30
  • 打赏
  • 举报
回复
??
UPDATE a SET date1=convert(varchar(11),date1,120)+' 17:00:00' FROM [Table] a
WHERE NOT exists(SELECT 1 FROM [Table] WHERE 用户名=a.用户名
and convert(varchar(20),date1,120) between convert(VARCHAR(11),date1,120)+'16:20:00' AND convert(VARCHAR(11),date1,120)+'17:00:00'
AND convert(varchar(20),date1,120)> convert(varchar(20),a.date1,120)
)
AND convert(varchar(8),date1,108) BETWEEN '16:20:00' AND '17:00:00'
BILLSSJONE 2010-04-30
  • 打赏
  • 举报
回复
那個時間格式不是只有小時的,有年月日小時分鐘的,時間格式是:smalldatetime
BILLSSJONE 2010-04-30
  • 打赏
  • 举报
回复
數據庫是sql server2005,假如就3個這段:
自動編號:key
添加時間:date1
用戶名:username
--小F-- 2010-04-30
  • 打赏
  • 举报
回复
update
tb
set
date1 = '17:00'
from
tb t
where
date1
between
'16:20' and '17:00'
and
not exists(select 1 from tb where 用户ID = t.用户ID and date1>t.date1 and date1 between '16:20' and '17:00' )
dawugui 2010-04-30
  • 打赏
  • 举报
回复
update tb set date1 = '17:00' from tb t where date1 between '16:20' and '17:00' and date1 = 
(select max(date1) from tb where date1 between '16:20' and '17:00' and 用户ID = t.用户ID )
永生天地 2010-04-30
  • 打赏
  • 举报
回复
再多给点字段信息,至少有没有主键,字段类型什么

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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