1
爲了ilustrate我的問題,考慮這三個表:#N×M個表查詢
人:
personid int auto_increment not null,
firstname varchar(16) not null,
constraint pk_person primary key (personid)
寵物:
petid int auto_increment not null,
petname varchar(16) not null,
constraint pk_pet primary key (petid)
所有權:
owner int not null,
pet int not null,
constraint fk_owner_ownership foreign key (owner) references Person (personid) on delete cascade,
constraint fk_pet_ownership foreign key (pet) references Pet (petid) on delete cascade,
constraint pk_ownership primary key (owner, pet)
和元組:
insert into person (firstname) values ("andy");
insert into person (firstname) values ("barney");
insert into person (firstname) values ("carly");
insert into pet (petname) values ("dog");
insert into pet (petname) values ("cat");
insert into ownership (owner, pet) values (1, 1); #andy owns a dog
insert into ownership (owner, pet) values (2, 2); #barney owns a cat
insert into ownership (owner, pet) values (3, 1);
insert into ownership (owner, pet) values (3, 2); #carly owns a dog and a cat
我想要一個查詢,只返回擁有狗和貓,在這種情況下是carly的所有者。寵物的數量可能超過這兩個。
thx爲答案!解決了我的問題。非常感謝 – patodomau