【发布时间】:2011-02-10 11:22:49
【问题描述】:
我有三个表:tblProduct、lkpFoodgroup、tblCustomer。还有一个联结表:jctCustomerFoodgroup
列是这样的:
**tblProduct**
+---+----------------+
|PK |int_ProductID |
|FK |int_FoodgroupID |
| |str_ProductName |
+---+----------------+
**lkpFoodgroup**
+---+-------------------+
|PK |int_FoodgroupID |
| |str_FoodgroupHandle|
+---+-------------------+
**tblCustomer**
+---+----------------+
|PK |int_CustomerID |
| |str_CustomerName|
+---+----------------+
**jctCustomerFoodgroup**
+---+----------------+
|PK |int_CustomerID |
|PK |int_FoodgroupID |
| |int_ProductID |
+---+----------------+
这些表中最简单的是查找:
**lkpFoodgroup**
+---------------+-------------------+
|int_FoodgroupID|str_FoodgroupHandle|
+---------------+-------------------+
|1 |fruit |
|2 |meat |
|3 |bread |
|4 |cheese |
+---------------+-------------------+
接下来是客户:
**tblCustomer**
+----------------+-------------------+
|int_CustomerID |str_CustomerName |
+----------------+-------------------+
|1 |Bob |
|2 |Sally |
|3 |Jane |
|4 |Billy |
+----------------+-------------------+
tblProduct 上可以有许多具有相同 Foodgroup 的产品。也可能有一些产品 Foodgroups 中没有产品:
**tblProduct**
+---------------+-----------------+----------------+
|int_ProductID |int_FoodgroupID |str_ProductName |
+---------------+-----------------+----------------+
|1 |1 |apple |
|2 |1 |banana |
|3 |1 |orange |
|4 |1 |pear |
|5 |2 |chicken |
|6 |2 |beef |
|7 |2 |fish |
|8 |2 |turkey |
|9 |3 |white |
|10 |3 |wheat |
+---------------+-----------------+----------------+
联结表上的 PK 是 int_CustomerID 和 int_FoodgroupID 的组合 - 这意味着任何客户只能为每个 Foodgroup 选择一个产品:
**jctCustomerFoodgroup**
+---------------+-----------------+--------------+------------------------+
|int_CustomerID |int_FoodgroupID |int_ProductID | --meaning |
+---------------+-----------------+--------------+------------------------|
|1 | 1 |1 | --Bob, fruit, apple |
|1 | 2 |6 | --Bob, meat, beef |
|1 | 3 |9 | --Bob, bread, white |
|2 | 1 |3 | --Sally, fruit, orange |
|2 | 2 |5 | --Sally, meat, chicken |
|3 | 1 |3 | --Jane, fruit, orange |
|3 | 3 |9 | --Jane, bread, white |
|3 | 2 |6 | --Jane, meat, beef |
+---------------+-----------------+--------------+------------------------+
我正在寻找一个查询,它会给我这样的结果:
**spGetCustomerProductSelections(1) --Get Bob's choices**
+----------------+---------------+-------------------+-------------+---------------+
|int_CustomerID |int_FoodgroupID|str_FoodgroupHandle|int_ProductID|str_ProductName|
+----------------+---------------+-------------------+-------------+---------------+
|1 |1 |fruit |1 |apple |
|1 |2 |meat |6 |beef |
|1 |3 |bread |9 |white |
|1 |4 |cheese |null |null |
+----------------+---------------+-------------------+-------------+---------------+
**spGetCustomerProductSelections(2) --Get Sally's choices**
+----------------+---------------+-------------------+-------------+---------------+
|int_CustomerID |int_FoodgroupID|str_FoodgroupHandle|int_ProductID|str_ProductName|
+----------------+---------------+-------------------+-------------+---------------+
|2 |1 |fruit |3 |orange |
|2 |2 |meat |5 |chicken |
|2 |3 |bread |null |null |
|2 |4 |cheese |null |null |
+----------------+---------------+-------------------+-------------+---------------+
**spGetCustomerProductSelections(4) --Get Billy's choices**
+----------------+---------------+-------------------+-------------+---------------+
|int_CustomerID |int_FoodgroupID|str_FoodgroupHandle|int_ProductID|str_ProductName|
+----------------+---------------+-------------------+-------------+---------------+
|4 |1 |fruit |null |null |
|4 |2 |meat |null |null |
|4 |3 |bread |null |null |
|4 |4 |cheese |null |null |
+----------------+---------------+-------------------+-------------+---------------+
有什么帮助吗?
【问题讨论】:
-
如果您生成创建和插入语句而不仅仅是数据,您将获得更大的吸引力。
-
由于 ProductID 是产品的主键,连接表 CustomerFoodGroup 应该是只有 ProductID 的 CustomerProduct,除非您的业务规则允许您针对不同的食品组购买产品。
标签: sql sql-server tsql