两张表
t_1, t_2
t_1:
ID MID Date
1 NULL 2007-1-21
2 NULL 2007-3-25
3 NULL 2007-3-26t_2:
ID Date
1 2007-1-22
2 2007-1-25
3 2007-1-29需求是这样的,把t_1作为目标表,需要更新MID,MID的值来自t_2表,拿t_1第一条记录跟t_2比较,发现t_2没有一条记录的date小于2007-1-21,所以转到下一条记录2007-3-25,发现t_2中有2007-1-25和2007-1-29两条数据符合,但2007-1-29的日期更接近2007-3-25,把t_2的ID:3更新到t_1的ID:2的MID中,转到t_1的2007-3-26,因为t_2的2007-1-29已经给之前的数据更新过,所以不参与这次的比较,t_1:2007-3-26只跟t_2:2007-1-12和2007-1-25比较,所以应该把t_2:2007-1-25的ID:2更新到t_1:ID=3的MID中,得到最终结果为
t_1
ID MID Date
1 NULL 2007-1-21
2 3 2007-3-25
3 2 2007-3-26个人认为仅靠1条sql无法达到这种需求,勉强做了这么一个SQL:
UPDATE a
SET mappingid =
(SELECT TOP 1 id
FROM t_2 b
WHERE b.date < a.date AND b.id NOT IN
(SELECT mappingid
FROM t_1
WHERE mappingid IS NOT NULL)
ORDER BY b.date DESC)
FROM t_1 a
WHERE a.mappingid IS NULL发现,(SELECT TOP 1 id
FROM t_2 b
WHERE b.date < a.date AND b.id NOT IN
(SELECT mappingid
FROM t_1
WHERE mappingid IS NOT NULL)
ORDER BY b.date DESC)这条语句得出的集合基本没有变。各位兄弟姐妹,这种需求能用一条sql实现吗?谢谢了
t_1, t_2
t_1:
ID MID Date
1 NULL 2007-1-21
2 NULL 2007-3-25
3 NULL 2007-3-26t_2:
ID Date
1 2007-1-22
2 2007-1-25
3 2007-1-29需求是这样的,把t_1作为目标表,需要更新MID,MID的值来自t_2表,拿t_1第一条记录跟t_2比较,发现t_2没有一条记录的date小于2007-1-21,所以转到下一条记录2007-3-25,发现t_2中有2007-1-25和2007-1-29两条数据符合,但2007-1-29的日期更接近2007-3-25,把t_2的ID:3更新到t_1的ID:2的MID中,转到t_1的2007-3-26,因为t_2的2007-1-29已经给之前的数据更新过,所以不参与这次的比较,t_1:2007-3-26只跟t_2:2007-1-12和2007-1-25比较,所以应该把t_2:2007-1-25的ID:2更新到t_1:ID=3的MID中,得到最终结果为
t_1
ID MID Date
1 NULL 2007-1-21
2 3 2007-3-25
3 2 2007-3-26个人认为仅靠1条sql无法达到这种需求,勉强做了这么一个SQL:
UPDATE a
SET mappingid =
(SELECT TOP 1 id
FROM t_2 b
WHERE b.date < a.date AND b.id NOT IN
(SELECT mappingid
FROM t_1
WHERE mappingid IS NOT NULL)
ORDER BY b.date DESC)
FROM t_1 a
WHERE a.mappingid IS NULL发现,(SELECT TOP 1 id
FROM t_2 b
WHERE b.date < a.date AND b.id NOT IN
(SELECT mappingid
FROM t_1
WHERE mappingid IS NOT NULL)
ORDER BY b.date DESC)这条语句得出的集合基本没有变。各位兄弟姐妹,这种需求能用一条sql实现吗?谢谢了
解决方案 »
- SQL 多行相同值,只显示第一行的数据
- 求一条SQL语句
- sql server 2008用户权限设置 大家帮帮我,郁闷死了!!
- 关于数据库建模的问题!
- 怎么用VB获得数据库表的字段个数和各字段的字段名(包括无记录的表和有记录的表)?
- SQL语句:判断如果字段state的值为空,那么就取这行数据,可是在数据库中用=NULL 或者='' 都不可以,谁可以告诉我怎么写??
- 请问怎样在查询分析器里执行UPDATE等语句后不显示‘(所影响的行数为 XX 行)’
- 问一句sql的写法
- 如何把文档等文件上传到SQL中呢?谢谢!
- 查询某个字段值是否包含在某个字符串中。【查询求助】
- 查询结果如何连列名一起保存?
- select * from aaa
insert t_1 select 1, NULL, '2007-1-21'
insert t_1 select 2, NULL, '2007-3-25'
insert t_1 select 3, NULL, '2007-3-26' create table t_2(ID int, Date datetime)
insert t_2 select 1, '2007-1-22'
insert t_2 select 2, '2007-1-25'
insert t_2 select 3, '2007-1-29'
go
declare test cursor for
select ID,[Date] from t_1
declare @ID int,@Date datetime
open test
fetch next from test into @ID,@Date
while @@fetch_status=0
begin
update t_1
set MID=(select ID from t_2 where Date=(select max(Date) from T_2 where Date<t_1.Date and ID not in(select isnull(MID,'') from t_1)))
where
ID=@ID
fetch next from test into @ID,@Date
end
close test
deallocate test--drop table dbo.t_2
select * from t_1ID MID Date
----------- ----------- ------------------------------------------------------
1 NULL 2007-01-21 00:00:00.000
2 3 2007-03-25 00:00:00.000
3 2 2007-03-26 00:00:00.000(所影响的行数为 3 行)
我从来没有在sql中使用游标,真的很惊叹于这种,也非常佩服您的sql功底,确实了得啊
谢谢你了。
insert t_1 select 1, NULL, '2007-1-21'
insert t_1 select 2, NULL, '2007-3-25'
insert t_1 select 3, NULL, '2007-3-26' create table t_2(ID int, Date datetime)
insert t_2 select 1, '2007-1-22'
insert t_2 select 2, '2007-1-25'
insert t_2 select 3, '2007-1-29'
--select a.*,b.*
update a set mid=b.id
from t_1 a
left join t_2 b
on b.date=
(select max(date) from t_2 x
where date<=a.date
and not exists(select 1 from t_1 y
where id<a.id and date>=x.date
and not exists(select 1 from t_2 where date>x.date and date<=y.date)
)
)
select * from t_1drop table t_1,t_2
你的这个sql正是我想要实现而不能实现的,这种效率更高。
能解释以下为什么我写的那个SQL实现错误吗?谢谢了,暂时只能在4天后才能加分给你了。
max(data) 和你的 top 1 data order by data desc是一个道理.