Table: Person
+-------------+---------+| Column Name | Type |+-------------+---------+| PersonId | int || FirstName | varchar || LastName | varchar |+-------------+---------+PersonId is the PRimary key column for this table.Table:
Address
+-------------+---------+| Column Name | Type |+-------------+---------+| AddressId | int || PersonId | int || City | varchar || State | varchar |+-------------+---------+AddressId is the primary key column for this table.Write a SQL query for a report that provides the following information for each person in the Person table, regardless if there is an address for each of those people:
FirstName, LastName, City, State這里要求不論Address信息有沒有都要保留Person的信息,這里是left join.
select Person.FirstName, Person.LastName, Address.City, Address.State from Personleft join Addresson Person.PersonId=Address.PersonId;SQL 連接:1. INNER JOIN:如果表中有至少一個匹配,則返回行;
2. LEFT JOIN:即使右表中沒有匹配,也從左表返回所有的行;
3. RIGHT JOIN:即使左表中沒有匹配,也從右表返回所有的行
來自:點擊打開鏈接
新聞熱點
疑難解答