Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 相同表字段之间的兼容性_Sql_Sql Server - Fatal编程技术网

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表连接起来,以获取店铺信息