一 : 创建表( 账号表和登录表)
1 账号表:
create table account ---创建账号表
(
account_id int identity(1,1),---账号id
account_name varchar(30),----账号名
)
2 插入数据
insert into account (account_name) values ('xw');
insert into account (account_name) values ('hc');
insert into account (account_name) values ('lc');
二 创建登录表
1 登录表
create table account_login
(
id int identity(1,1),
account_id int,
login_time datetime
)
2 插入数据
insert into account_login(account_id,login_time)values(2,'2017-02-12 10:00:00');
insert into account_login(account_id,login_time)values(3,'2017-02-12 14:00:00');
insert into account_login(account_id,login_time)values(4,'2017-02-12 09:00:00');
三 连接查询
1 找出创建了账号,但未登录游戏的玩家
需要用左连接: 左连接表示以left join左边的表为准,只要在左边表里出现的数据,在查询表里都会出现。( 如果左边表的数据在右边没有关联数据,则查询结果里右边列的数据为null).
a 查询语句
select * from account
left join account_login
on account.account_id = account_login.account_id
b 结果显示
2 查找 账号表中遗漏的数据(即在登录表中有数据,但在账号表中没有数据)
需要用右连接: 右连接表示以right join右边的表为准,只要在右边表里出现的数据,在查询表里都会出现。( 如果右边表的数据在左边没有关联数据,则查询结果里左边表的列的数据为null).
a 查询语句:
select * from account
right join account_login
on account.account_id = account_login.account_id
b 查询结果
3 全连接: 两个表中的数据都会显示出来
a 查询语句
select * from account
full join account_login
on account.account_id = account_login.account_id
b 查询结果
4 内连接:两个表中有关联的项目才会显示出来
a 查询语句
select * from account
join account_login
on account.account_id = account_login.account_id
b 查询结果