需求:编写一个 SQL 查询,对两表进行关联,展示列为: FirstName, LastName, City, State
展示效果:
LastName | City | State | |
---|---|---|---|
Allen | Wang | New York City | New York |
1 Create table Person (PersonId int, FirstName varchar(255), LastName varchar(255)); 2 3 Create table Address (AddressId int, PersonId int, City varchar(255), State varchar(255)); 4 5 insert into Person (PersonId, LastName, FirstName) values (1, ‘Wang‘, ‘Allen‘); 6 insert into Address (AddressId, PersonId, City, State) values (1, 1, ‘New York City‘, ‘New York‘);
最终SQL:
1 select 2 p.FirstName, 3 p.LastName, 4 a.City, 5 a.State 6 from 7 Person as p 8 left join 9 Address as a 10 on 11 p.PersonId = a.PersonId;