SQL Fiddle
MS SQL Server 2017 架构设置:
create table my_user (
ID int primary key,
FirstName varchar(255),
LastName varchar(255),
address varchar(max)
)
create table user_phones (
ID int primary key,
userId int references my_user(ID),
countrycode int,
phone varchar(max),
type varchar(255)
)
INSERT INTO my_user(ID,FirstName,LastName,address) VALUES(1,'test','test','test');
INSERT INTO user_phones(ID,userId,countrycode,phone,type)VALUES(1,1,99,'000099900','mobile');
INSERT INTO user_phones(ID,userId,countrycode,phone,type)VALUES(2,1,99,'99900000','home');
INSERT INTO user_phones(ID,userId,countrycode,phone,type)VALUES(3,1,99,'000009999','fax');
查询 1:
select * from my_user u
left join user_phones p on u.ID=p.userId
Results:
| ID | FirstName | LastName | address | ID | userId | countrycode | phone | type |
|----|-----------|----------|---------|----|--------|-------------|-----------|--------|
| 1 | test | test | test | 1 | 1 | 99 | 000099900 | mobile |
| 1 | test | test | test | 2 | 1 | 99 | 99900000 | home |
| 1 | test | test | test | 3 | 1 | 99 | 000009999 | fax |