一个简单的三表连接查询,在线等
--一个简单的三表连接查询,表结构如下
--个人表
create table person(
pid int primary key identity(1,1),
personName varchar(20) not null,
)
--物品表
create table things(
tid int primary key identity(1,1),
thingName varchar(20)
)
--关联人和物品
create table myThings(
pid int not null,
tid int not null,
)
insert into person values('tom')
insert into person values('jerry')
insert into person values('jack')
insert into things values('phone')
insert into things values('computer')
insert into things values('camera')
insert into myThings values(1,2)
insert into myThings values(1,3)
insert into myThings values(1,1)
insert into myThings values(2,2)
insert into myThings values(2,3)
insert into myThings values(3,1)
/*
我想要的实现功能如下
如果要显示有phone的人,显示tom和jack
如果要显示有computer,camera的人,则是tom和jerry
如果要显示3种物品都有的人,则只有一个tom
要求只用一条SQL语句搞定以上3种功能
或者设计其他便于这种查询的表结构
*/