2011-02-10 17 views
0

私は3つのテーブルtblProduct、lkpFoodgroup、tblCustomerを持っています。そして、もう一つジャンクションテーブル:jctCustomerFoodgroup3つのテーブルに参加してヌル値を取得しようとしています

列は、このようなものです:

**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につき1つの製品を選択することができることを意味している:

**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   | 
+----------------+---------------+-------------------+-------------+---------------+ 

任意の助けを?

+0

データの代わりにcreate文とinsert文を生成した場合、より多くのトラクションが得られます。 – RichardTheKiwi

+1

[WTF much](http://thedailywtf.com/Articles/adjYour_adjDaily_nCup_prOf_adjAkWTF.aspx)? – outis

+0

ProductIDが製品の主キーであるため、異なる食品グループに対して製品を購入できるビジネスルールがない限り、接合テーブルCustomerFoodGroupはProductIDのみである必要があります。 – Nat

答えて

0

"sp"で始まる手続きの名前を付けないでください。マスターデータベースでの検索が開始され、後でデータベースに戻されます。

あなたのスキーマのDDLとデータ

create table lkpFoodgroup 
(int_FoodgroupID int, str_FoodgroupHandle varchar(max)) 
insert lkpFoodgroup values 
(1,'fruit'), 
(2,'meat'), 
(3,'bread'), 
(4,'cheese'); 

create table tblCustomer 
(int_CustomerID int, str_CustomerName varchar(max)) 
insert tblCustomer values 
(1,'Bob'), 
(2,'Sally'), 
(3,'Jane'), 
(4,'Billy'); 

create table tblProduct 
(int_ProductID int, int_FoodgroupID int, str_ProductName varchar(max)) 
insert tblProduct values 
(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'); 

create table jctCustomerFoodgroup 
(int_CustomerID int, int_FoodgroupID int, int_ProductID varchar(max)) 
insert jctCustomerFoodgroup values 
(1,'1','1'), 
(1,'2','6'), 
(1,'3','9'), 
(2,'1','3'), 
(2,'2','5'), 
(3,'1','3'), 
(3,'3','9'), 
(3,'2','6'); 

ストアドプロシージャのコード

create proc uspGetCustomerProductSelections @customerID int as 
select c.int_CustomerID, l.int_FoodgroupID, l.str_FoodgroupHandle, j.int_ProductID, p.str_ProductName 
from tblCustomer c 
cross join lkpFoodgroup l 
left join jctCustomerFoodgroup j on j.int_CustomerID = c.int_CustomerID and j.int_FoodgroupID = l.int_FoodgroupID 
left join tblProduct p on p.int_ProductID = j.int_ProductID 
where c.int_CustomerID = @customerID 

サンプルの実行

exec uspGetCustomerProductSelections 1 

出力

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 
+0

あなたは "sp_"を意味しましたか? – Nat

+0

ええ、ルールは私がそれらの名前をつけることはできないと思っていたのですが、私はspは大丈夫だと思いました。 –

+0

cyberkiwiさん、ありがとうございました。 –

0

外部結合(左、右または完全)を使用する必要があります。これらの結合にはNULL値が含まれます。

関連する問題