Sql 相同表字段之间的兼容性
我有商店类型。某些商店类型与其他商店类型不兼容(例如,您不能在食品附近销售汽车零件) 以下是我的表模式:Sql 相同表字段之间的兼容性,sql,sql-server,Sql,Sql Server,我有商店类型。某些商店类型与其他商店类型不兼容(例如,您不能在食品附近销售汽车零件) 以下是我的表模式: create table TShopCompatibility ( idshoptype1 int NOT NULL, idshoptype2 int NOT NULL, constraint pkSHOPCOMP primary key(idshoptype1,idshoptype2), constraint fkSHOPCOMP1 foreign key(idshoptype1) ref
create table TShopCompatibility
(
idshoptype1 int NOT NULL,
idshoptype2 int NOT NULL,
constraint pkSHOPCOMP primary key(idshoptype1,idshoptype2),
constraint fkSHOPCOMP1 foreign key(idshoptype1) references TShopType(idshoptype),
constraint fkSHOPCOMP2 foreign key(idshoptype2) references TShopType(idshoptype),
constraint cSHOPCOMP12 check(idshoptype1>idshoptype2)
)
我有这些价值观:
2 - 1
3 - 1
5 - 1
5 - 2
10 - 9
12 - 11
13 - 10
How where id-shoptypes。如何获取与idshoptype=2兼容的商店?您需要选择在idshoptype1或idshoptype2中存在
2
的商店类型,即
SELECT idshoptype1 FROM TShopCompatibility WHERE idshoptype2 = 2
UNION
SELECT idshoptype2 FROM TShopCompatibility WHERE idshoptype1 = 2
然后,您可以将此查询的结果与shops表连接起来,以获取店铺信息