2013-04-08 72 views
2

正如標題所示,我需要一些指導。目前我所擁有的是相當接近的,只是每種顏色之一都在重複着自己,我似乎無法擺脫困境。根據所有其他顏色生成顏色列表

這裏是我的表:

CREATE TABLE Colors 
    (c_ID VARCHAR2(3) NOT NULL, 
    c_NAME VARCHAR2(11)); 

    INSERT INTO Colors VALUES 
    ('T01','RED'); 
    INSERT INTO Colors VALUES 
    ('T02','BLUE'); 
    INSERT INTO Colors VALUES 
    ('T03','BLACK'); 
    INSERT INTO Colors VALUES 
    ('T04','YELLOW'); 
    INSERT INTO Colors VALUES 
    ('T05','ORANGE'); 

,我使用的查詢:

select distinct a.c_name as "HOME", s.c_name as "AWAY" 
from colors a, colors s 
order by a.c_name; 

結果都爲:

Black  Black 
Black  Blue 
Black  Orange 
Black  Red 
Black  Yellow 

這會爲每個顏色,但你可以看到總有一種情況,每種顏色都會反覆出現。我怎樣才能擺脫那不刪除?提前致謝。

+2

添加此'其中a.t_name <> s.t_name' – 2013-04-08 16:46:58

+1

好了,你的問題得到了有效回答。只需要提供一些東西,就可以在'INSERT INTO Colors(c_ID,c_NAME)VALUES('T01','RED'),('T02','BLUE'),('T03','BLACK '),('T04','YELLOW'),('T05','ORANGE');' – 2013-04-08 17:02:56

回答

2

你可以只改變你的查詢是這樣的:

select distinct a.t_name as "HOME", s.t_name as "AWAY" 
from teams a, teams s 
where a.t_name <> s.t_name -- or this if you need to check on id instead a.t_id <> s.t_id 
order by a.t_name; 
+1

優秀,歡呼,8分鐘接受! :) – Ryoss 2013-04-08 16:51:14