我有两个标准化的SQL Server 2008表,一个用于名称,另一个用于电子邮件:
create table Name (NameId int, Name varchar(50))
create table Email (NameId int, Email varchar(50))
go
insert into Name values (1, 'JOHN SMITH')
insert into Name values (2, 'MARY SMITH')
insert into Name values (3, 'PHILL TAYLOR')
go
insert into Email values (1, 'john@hotmail.com')
insert into Email values (1, 'john@gmail.com')
insert into Email values (1, 'john.smith@hotmail.com')
insert into Email values (2, 'mary@hotmail.com')
insert into Email values (3, 'phill@hotmail.com')
insert into Email values (3, 'phill@gmail.com')
insert into Email values (3, 'taylor.phill@hotmail.com')
insert into Email values (3, 'taylor.phill@gmail.com')
go
当我加入这些表时,我有几行,其中只有一封电子邮件:
Select name, email
from Name inner join Email on name.NameId=email.NameId
NAME EMAIL
------------- --------------------------
JOHN SMITH john@hotmail.com
JOHN SMITH john@gmail.com
JOHN SMITH john.smith@hotmail.com
MARY SMITH mary@hotmail.com
PHILL TAYLOR phill@hotmail.com
PHILL TAYLOR phill@gmail.com
PHILL TAYLOR taylor.phill@hotmail.com
PHILL TAYLOR taylor.phill@gmail.com
但是我需要将所有电子邮件放在同一行中以满足预定义的布局:
NAME EMAIL1 EMAIL2 EMAIL3 EMAIL4
------------- ----------------- --------------- ----------------------- -----------------
JOHN SMITH john@hotmail.com john@gmail.com john.smith@hotmail.com
MARY SMITH mary@hotmail.com
PHILL TAYLOR phill@hotmail.com phill@gmail.com taylor.phill@hotmail.com taylor.phill@hotmail.com
是否可以使用单选?
任何想法将不胜感激
TIA
里卡多·诺盖拉