力扣数据库mysql 简单题
1.组合两张表
表: Person
+-------------+---------+ | 列名 | 类型 | +-------------+---------+ | PersonId | int | | FirstName | varchar | | LastName | varchar | +-------------+---------+
表: Address
+-------------+---------+ | 列名 | 类型 | +-------------+---------+ | AddressId | int | | PersonId | int | | City | varchar | | State | varchar | +-------------+---------+
SQL语句:
create table department(
did int(4) not null primary key,
danme varchar(36)
);
CREATE table employee(
id int(4) not null primary key,
name varchar(36),
age int(2),
did int(4) not null
)
insert into department values(1,'网络部'),(2,'媒体部'),(5,'人事部');
insert into employee values(1,'王红',20,1),(2,'李强',22,1),(3,'赵四',20,2),(4,'郝娟',20,4);

select employee.name,department.danme
from department join employee
on department.did = employee.did;
上述和where子句差不多
外连接:(左连接)
select department.did,department.danme,employee.name from department left JOIN
employee on department.did=employee.did;
外连接:(右连接)
select department.did,department.danme,employee.name from department right JOIN
employee on department.did=employee.did;