日期:2014-05-18 浏览次数:20612 次
create table ta
(id int identity(1,1),
slot varchar(50),
isbn varchar(50),
qty int)
create table tb
(isbn varchar(50),
qty int)
insert ta(slot,isbn,qty)
select 'a ', '001 ',10
union all
select 'b ', '002 ',5
union all
select 'c ', '001 ',6
union all
select 'd ', '001 ',6
union all
select 'e ', '003 ',8
union all
select 'f ', '002 ',10
union all
select 'd ', '004 ',8
insert tb(isbn,qty)
select '001 ',12
union all
select '002 ',8
union all
select '003 ',7
select id=identity(int),a.slot,a.isbn,a.qty qty_ta,c.qty qty_tb
into #temp
from ta a
inner join
(select min(id) mi from ta group by slot) b
on id=mi
inner join tb c
on c.isbn=a.isbn order by a.isbn
select * from #temp
drop table ta,tb,#temp
------解决方案--------------------
q2
select mi id_ta,a.slot,a.isbn,a.qty qty_ta,c.qty qty_tb
from tb c
inner join ta a
on a.isbn=c.isbn
inner join
(select min(id) mi from ta group by isbn) b
on a.id=mi
order by a.isbn