sql的问题,确定位置

iceman1983 2008-01-18 04:32:26
表一 (第二列没有用)
块 col2 行 列 id
a 1 1 1 asd1
a 1 1 2 asd2
a 1 1 3 asd3
a 1 1 4 asd4
a 1 1 5 asd5
a 1 1 6 asd6
a 1 2 1 asd7
a 1 2 2 asd8
a 1 2 3 asd9
a 1 2 4 asd10
a 1 3 1 asd11
a 1 3 2 asd12
b 1 1 1 asd13
b 1 1 2 asd24
b 1 1 3 asd35
b 1 2 1 asd46

表2
块 id
a asd88
b asd54
问题
想把表2的数据插入到表1里面,表一前四列表示位置,要把表2的数据插入到表一相同的块里面,具体位置是该块上 拥有最少列的行的末尾,比如说例子里面就应该把
块 col2 行 列 id
a 1 3 3 asd88
b 1 2 2 asd54
插入到表中去,但是我怎么才能通过sql确定这个位置呢,

...全文
130 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
-狙击手- 2008-01-18
  • 打赏
  • 举报
回复
create table tb(块 varchar(10),行 int,列 int,id varchar(10))
insert into tb
select 'a',1,1,'asd1'
union
select 'a',1,2,'asd2'
union
select 'a',1,3,'asd3'
union
select 'a',1,4,'asd4'
union
select 'a',1,5,'asd5'
union
select 'a',1,6,'asd6'
union
select 'a',2,1,'asd7'
union
select 'a',2,2,'asd8'
union
select 'a',2,3,'asd9'
union
select 'a',2,4,'asd10'
union
select 'a',3,1,'asd11'
union
select 'a',3,2,'asd12'
union
select 'b',1,1,'asd13'
union
select 'b',1,2,'asd24'
union
select 'b',1,3,'asd35'
union
select 'b',2,1,'asd46'

create table tb2 (块 varchar(8),id varchar(10))

insert into tb2
select 'a','asd88'
union
select 'b','asd54'

insert tb
select 块,(select max(行) from tb where 块 = b.块) ,
(select top 1 列 + 1from tb where 块 = b.块 order by 行 desc,列 desc ),id from tb2 b

select * from tb

drop table tb,tb2
/*


块 行 列 id
---------- ----------- ----------- ----------
a 1 1 asd1
a 1 2 asd2
a 1 3 asd3
a 1 4 asd4
a 1 5 asd5
a 1 6 asd6
a 2 1 asd7
a 2 2 asd8
a 2 3 asd9
a 2 4 asd10
a 3 1 asd11
a 3 2 asd12
b 1 1 asd13
b 1 2 asd24
b 1 3 asd35
b 2 1 asd46
a 3 3 asd88
b 2 2 asd54

(所影响的行数为 18 行)

*/
rouqu 2008-01-18
  • 打赏
  • 举报
回复
具体位置是该块上 拥有最少列的行的末尾
-----------------
看不懂 为了UP而接分 ^_^
谁是谁的谁 2008-01-18
  • 打赏
  • 举报
回复
路过,留个记号
hui_hui_2007 2008-01-18
  • 打赏
  • 举报
回复
关注.
areswang 2008-01-18
  • 打赏
  • 举报
回复
mark了
yangjiexi 2008-01-18
  • 打赏
  • 举报
回复

create table tb(块 varchar(10),行 int,列 int,id varchar(10))
insert into tb
select 'a',1,1,'asd1'
union
select 'a',1,2,'asd2'
union
select 'a',1,3,'asd3'
union
select 'a',1,4,'asd4'
union
select 'a',1,5,'asd5'
union
select 'a',1,6,'asd6'
union
select 'a',2,1,'asd7'
union
select 'a',2,2,'asd8'
union
select 'a',2,3,'asd9'
union
select 'a',2,4,'asd10'
union
select 'a',3,1,'asd11'
union
select 'a',3,2,'asd12'
union
select 'b',1,1,'asd13'
union
select 'b',1,2,'asd24'
union
select 'b',1,3,'asd35'
union
select 'b',2,1,'asd46'

create table tb2 (块 varchar(8),id varchar(10))

insert into tb2
select 'a','asd88'
union
select 'b','asd54'

insert into tb
select
a.块,
(select max(行) from tb d where d.块=a.块) as 行,
(select max(列)+1 from tb b
where b.块=a.块
and not exists (select 1 from tb c where c.块=b.块 and c.行>b.行)) as 列,
a.id
from tb2 a
yangjiexi 2008-01-18
  • 打赏
  • 举报
回复

insert into tb
select
a.块,
(select max(行) from tb d where d.块=a.块) as 行,
(select max(列)+1 from tb b
where b.块=a.块
and not exists (select 1 from tb c where c.块=b.块 and c.行>b.行)) as 列,
a.id
from tb2 a
dawugui 2008-01-18
  • 打赏
  • 举报
回复
insert into 表一
select a.块 , isnull(max(a.col2),1) , isnull(max(a.行),1) , isnull(max(a.列),1) + 1 , b.id from 表一 a,表2 b where a.块 = b.块 group by a.块
wzy_love_sly 2008-01-18
  • 打赏
  • 举报
回复
接分+up
不会这个
dawugui 2008-01-18
  • 打赏
  • 举报
回复
insert into 表一
select a.块 , isnull(max(a.col2),1) , isnull(max(a.行),1) , isnull(max(a.列),1) + 1 , b.id from 表一 a,表2 b where a.块 = b.块
dawugui 2008-01-18
  • 打赏
  • 举报
回复
SQL没有你这个说法.每次的显示是根据你的排序来定的.
昵称被占用了 2008-01-18
  • 打赏
  • 举报
回复
insert 表一(块,行,列,id)
select
块,
(select max(行) from 表一 where 块=a.块) as 行,
(select max(列)+1 from 表一 b where 块=a.块 and not exists (select 1 from 表一 where 块=b.块 and 行>b.行)) as 列,
id
from 表2 a

34,575

社区成员

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

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