两张表的连接
有一张表,Small, Small表中每一个advertiserId对应很多账号,账号放在表 account中。
两张表连接以后就是下边的表Big.
SQL怎么写?
--table Small
AdvertiserId GroupId
1 0
2 0
3 0
--table Big
AdvertiserId GroupId Account
1 0 101
1 0 102
1 0 103
2 0 201
2 0 987
2 0 9792
3 0 213
3 0 234
3 0 917
--Small
if object_id('Small','u') is not null
drop table Small
go
create table Small
(
AdvertiserId int,
GroupID int
)
go
insert into Small values
(1,0),
(2,0),
(3,0)
--Account
if object_id('Account','u') is not null
drop table Account
go
create table Account
(
AdvertiserId int,
AccountId int
)
go
insert into Account values
(1,101),
(1,102),
(1,103),
(2,201),
(2,987),
(2,9792),
(3,213),
(3,234),
(3,917)
go
--结果集
select Small.AdvertiserId,Small.GroupID,Account.AccountId Account from Small inner join Account on Small.AdvertiserId=Account.AdvertiserId
/*
10101
10102
10103
20201
20987
209792
30213
30234
30917
*/