2011-10-04 43 views
1

我有以下表:中顯示多類別單個條目

    ,其具有一個ID列,說明列和類別父ID列如下所示
  1. 分類表:

 
cat_id | cat_description | cat_pid 
-------+-----------------+-------- 
1  | State   | 0 
2  | Texas   | 1 
3  | California  | 1 
4  | Title   | 0 
5  | Engineer  | 4 
6  | Lawyer   | 4 
7  | Farmer   | 4 
8  | Credit Card  | 0 
9  | Visa   | 8 
10  | Master Card  | 8 
... 
2客戶名單中有客戶ID和姓名如下:

 
cust_id | cust_name
--------+----------- 111111 | John 222222 | David 333333 | Chris 444444 | Mark ...
3. Category_Has_Customer這是Custome之間的多對多關係r和類別。

 
chc_cust_id | chc_cat_id 
------------+----------- 
111111  | 2 
111111  | 5 
111111  | 9 
222222  | 7 
222222  | 3 
333333  | 6 
該類別只有兩個級別的深度。

在我的應用程序中,客戶可能有零個或多個類別。我想顯示客戶擁有的部分或全部類別。例如,如果我選擇顯示所有類別我想有如下表:

 
cust_name | State  | Title | Credit Card 
----------+------------+----------+------------ 
John  | Texas  | Engineer | Visa 
David  | California | Farmer | 
Chris  |   | Lawyer | 
Mark  |   |   | 
我也應該能顯示某些類別,例如標題,只有信用卡:

 
cust_name | Title | Credit Card 
----------+----------+------------ 
John  | Engineer | Visa 
David  | Farmer | 
Chris  | Lawyer | 
Mark  |   |

我試着用左手做加盟的,是這樣的:

SELECT c1.cust_id, c1.cust_name, t1.cat_desc as State 
FROM Customer c1, Category_has_Customer chc 
LEFT JOIN Category t1 ON t1.cat_pid = '1' AND chc.chc_cat_id = t1.cat_id 
WHERE c1.cust_id = chc.chc_cust_id 

但它並沒有幫助,因爲我得到了重複的行。

在此先感謝您的幫助

+1

哪個RDBMS是這樣的?某些解決方案不適用於所有平臺。 –

回答

0

我該給一去,應該對所有SQL implemtations工作,但我已經在T-SQL做過:

有了這些數據

declare @Category table (cat_id int, cat_description varchar(20), cat_pid int) 
declare @Customer table (cust_id int, cust_name varchar(20)) 
declare @Customer_Has_Category table (chc_cust_id int, chc_cat_id int) 

insert into @Category (cat_id, cat_description, cat_pid) values (1,'State',0) 
insert into @Category (cat_id, cat_description, cat_pid) values (2,'Texas',1) 
insert into @Category (cat_id, cat_description, cat_pid) values (3,'California',1) 
insert into @Category (cat_id, cat_description, cat_pid) values (4,'Title',0) 
insert into @Category (cat_id, cat_description, cat_pid) values (5,'Engineer',4) 
insert into @Category (cat_id, cat_description, cat_pid) values (6,'Lawyer',4) 
insert into @Category (cat_id, cat_description, cat_pid) values (7,'Farmer',4) 
insert into @Category (cat_id, cat_description, cat_pid) values (8,'Credit Card',0) 
insert into @Category (cat_id, cat_description, cat_pid) values (9,'Visa',8) 
insert into @Category (cat_id, cat_description, cat_pid) values (10,'Master Card',8) 

insert into @Customer (cust_id, cust_name) values (111111, 'John') 
insert into @Customer (cust_id, cust_name) values (222222, 'David') 
insert into @Customer (cust_id, cust_name) values (333333, 'Chris') 
insert into @Customer (cust_id, cust_name) values (444444, 'Mark') 

insert into @Customer_Has_Category (chc_cust_id, chc_cat_id) values (111111, 2) 
insert into @Customer_Has_Category (chc_cust_id, chc_cat_id) values (111111, 5) 
insert into @Customer_Has_Category (chc_cust_id, chc_cat_id) values (111111, 9) 
insert into @Customer_Has_Category (chc_cust_id, chc_cat_id) values (222222, 7) 
insert into @Customer_Has_Category (chc_cust_id, chc_cat_id) values (222222, 3) 
insert into @Customer_Has_Category (chc_cust_id, chc_cat_id) values (333333, 6) 

這個查詢

select cust_name, MAX(State) as State, MAX(Title) as Title, MAX(CreditCard) as CreditCard 
from 
(
select 
c.cust_name, 
(case when cat.cat_pid = 1 then cat_description else '' end) as State, 
(case when cat.cat_pid = 4 then cat_description else '' end) as Title, 
(case when cat.cat_pid = 8 then cat_description else '' end) as CreditCard 
from @Customer c 
left outer join @Customer_Has_Category chc on c.cust_id = chc.chc_cust_id 
left outer join @Category cat on chc.chc_cat_id = cat.cat_id 
) as subq 
group by cust_name 

給出

cust_name   State    Title    CreditCard 
-------------------- -------------------- -------------------- -------------------- 
Chris          Lawyer    
David    California   Farmer    
John     Texas    Engineer    Visa 
Mark 

如果你想取出狀態,只需從select語句中刪除它。

+0

非常感謝您的快速響應! 這正是我所期待的:) – user979353

+0

沒問題,它解決了你的問題嗎? – amelvin

+0

它確實如此:)我試圖做一個星期,並搜索網絡,但沒有運氣。現在這個問題已經解決了,謝謝你 – user979353