如何插入临时表数据

hjl13960 2011-09-01 08:55:13
我有个表
User(Id,year,month,name,sex)
1,2001, 11,‘张三’,'男'
2,2011, 01,‘张三’,'男'
3,2011, 11,‘李四’,'男'
4,2001, 06,‘李四’,'男'
5,2011, 11,‘张三’,'男'
然后我要把这个表的数据插入临时表
这个临时表最后获取到的数据的id为2,3,5

条件是这样的,获取year和month最晚的数据,也就是时间最晚,
然后多个条件name like '%'+@name+'%' and sex like '%' +@sex+'%'
所以最后如果name是张三,sex是男的话,最后临时表的数据,就是user表里Id为2的数据了

...全文
428 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
geniuswjt 2011-09-01
  • 打赏
  • 举报
回复
就是如果记录中名字和姓名都相同则取时间最近的1条。
自表联接。
hjl13960 2011-09-01
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 geniuswjt 的回复:]

SQL code

if object_id('[User]') is not null drop table [User]
create table [User] (Id int,year int,month varchar(2),name varchar(4),sex varchar(2))
insert into [User]
select 1,2001,'11','张三','男' un……
[/Quote]
能帮忙解释下嘛
hjl13960 2011-09-01
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 geniuswjt 的回复:]

SQL code

if object_id('[User]') is not null drop table [User]
create table [User] (Id int,year int,month varchar(2),name varchar(4),sex varchar(2))
insert into [User]
select 1,2001,'11','张三','男' un……
[/Quote]

这位大哥做出我要得效果,可惜小弟没明白

where not exists
(
select 1 from [User] where name=a.name and sex=a.sex
and ltrim(year)+ltrim(month)>ltrim(a.year)+ltrim(a.month)
)
--小F-- 2011-09-01
  • 打赏
  • 举报
回复
select
*
from
[user] a
where
not exists(select 1 from [user] where name=a.name and sex=a.sex
and [year]>a.[year] or (name=a.name and sex=a.sex
and [year]=a.[year] and [month]>a.[month]))
geniuswjt 2011-09-01
  • 打赏
  • 举报
回复
9楼看眼[Quote=引用 10 楼 hjl13960 的回复:]
呵呵,这样吧,各位就不要管@name,@sex 。
[/Quote]
hjl13960 2011-09-01
  • 打赏
  • 举报
回复
呵呵,这样吧,各位就不要管@name,@sex 。
geniuswjt 2011-09-01
  • 打赏
  • 举报
回复

if object_id('[User]') is not null drop table [User]
create table [User] (Id int,year int,month varchar(2),name varchar(4),sex varchar(2))
insert into [User]
select 1,2001,'11','张三','男' union all
select 2,2011,'01','张三','男' union all
select 3,2011,'11','李四','男' union all
select 4,2001,'06','李四','男' union all
select 5,2011,'11','王五','男'

select * /* into #t */ from [User] a
where not exists
(
select 1 from [User] where name=a.name and sex=a.sex
and ltrim(year)+ltrim(month)>ltrim(a.year)+ltrim(a.month)
)

drop table [User]

/*
2 2011 01 张三 男
3 2011 11 李四 男
5 2011 11 王五 男
geniuswjt 2011-09-01
  • 打赏
  • 举报
回复

select * into #t from User a
where not exists
(
select 1 from User where name=a.name and sex=a.sex
and ltrim(year)+ltrim(month)>ltrim(a.year)+ltrim(a.month)
)
hjl13960 2011-09-01
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 geniuswjt 的回复:]

SQL code

select top 1 * into #t from User
where name like '%'+@name+'%' and sex like '%' +@sex+'%'
--这样?不过为什么要用like,而且前后都%?用不到索引诶,名字还能模糊的?
--然后你要的结果是2,3,5。为什么又要传@name和@sex?那不是会唯一确定一个的?
[/Quote]

谢谢哦,我只是比喻一下,问题会比较清楚
geniuswjt 2011-09-01
  • 打赏
  • 举报
回复

--楼主意思是不是就是相同性别和名字的取最新时间的插入临时表嘛,是的话如下:
select * into #t from User a
where not exists
(select 1 from User where name=a.name and sex=a.sex and year>a.year)
--如果还是要传变量自行改下好了
-晴天 2011-09-01
  • 打赏
  • 举报
回复
create table [User](Id int,[year] int,[month] int,name nvarchar(10),sex varchar(10))
insert into [User] select 1,2001, 11,'张三','男'
insert into [User] select 2,2011, 01,'张三','男'
insert into [User] select 3,2011, 11,'李四','男'
insert into [User] select 4,2001, 06,'李四','男'
insert into [User] select 5,2011, 11,'张三','男'
go
select * from [user] a where not exists(select 1 from [user] where [year]>a.[year] or [year]=a.[year] and [month]>a.[month])
/*
Id year month name sex
----------- ----------- ----------- ---------- ----------
3 2011 11 李四 男
5 2011 11 张三 男

(2 行受影响)

*/
go
drop table [user]

另外,你的 @name,@sex 值是什么?
hjl13960 2011-09-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 qianjin036a 的回复:]

2 可不是时间最晚,比2011年01月晚年有2011年11月呢.
[/Quote]
是的,我写错了5.的名字应该是"王五“
geniuswjt 2011-09-01
  • 打赏
  • 举报
回复

order by year desc
geniuswjt 2011-09-01
  • 打赏
  • 举报
回复

select top 1 * into #t from User
where name like '%'+@name+'%' and sex like '%' +@sex+'%'
--这样?不过为什么要用like,而且前后都%?用不到索引诶,名字还能模糊的?
--然后你要的结果是2,3,5。为什么又要传@name和@sex?那不是会唯一确定一个的?
-晴天 2011-09-01
  • 打赏
  • 举报
回复
2 可不是时间最晚,比2011年01月晚年有2011年11月呢.

22,209

社区成员

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

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