select DISTINCT leg.destination,leg.departure from trktripleg leg where leg.T# IN (select T# from trktrip trip WHERE L#='10001' OR L#='10002');
以上是我执行的sql语句...
查找驾照号码 (L#) 10001 和 10002 的两位司机至少访问过一次的所有城市(出发地或目的地)的名称。
表说明:
SQL> desc trktrip;
Name Null? Type
----------------------------------------- -------- ----------------------------
T# NOT NULL NUMBER(10)
L# NOT NULL NUMBER(8)
REG# NOT NULL VARCHAR2(10)
TRKTRIP_DATE NOT NULL DATE
SQL> desc trktripleg;
Name Null? Type
----------------------------------------- -------- ---------------------------
T# NOT NULL NUMBER(10)
LEG# NOT NULL NUMBER(2)
DEPARTURE NOT NULL VARCHAR2(30)
DESTINATION NOT NULL VARCHAR2(30)
一些样本数据:
在 trktripleg 上插入数据
INSERT INTO TRKTRIPLEG VALUES( 1, 1, 'Sydney', 'Melbourne');
INSERT INTO TRKTRIPLEG VALUES( 1, 2, 'Melbourne', 'Adelaide');
INSERT INTO TRKTRIPLEG VALUES( 1, 3, 'Adelaide', 'Sydney');
INSERT INTO TRKTRIPLEG VALUES( 2, 1, 'Sydney', 'Melbourne');
INSERT INTO TRKTRIPLEG VALUES( 2, 2, 'Melbourne', 'Adelaide');
INSERT INTO TRKTRIPLEG VALUES( 2, 3, 'Adelaide', 'Sydney');
INSERT INTO TRKTRIPLEG VALUES( 3, 1, 'Sydney', 'Melbourne');
INSERT INTO TRKTRIPLEG VALUES( 3, 2, 'Melbourne', 'Adelaide');
INSERT INTO TRKTRIPLEG VALUES( 3, 3, 'Adelaide', 'Sydney');
INSERT INTO TRKTRIPLEG VALUES( 4, 1, 'Sydney', 'Melbourne');
INSERT INTO TRKTRIPLEG VALUES( 4, 2, 'Melbourne', 'Adelaide');
INSERT INTO TRKTRIPLEG VALUES( 4, 3, 'Adelaide', 'Sydney');
INSERT INTO TRKTRIPLEG VALUES( 5, 1, 'Melbourne', 'Sydney');
INSERT INTO TRKTRIPLEG VALUES( 5, 2, 'Sydney', 'Adelaide');
INSERT INTO TRKTRIPLEG VALUES( 5, 3, 'Adelaide', 'Sydney');
INSERT INTO TRKTRIPLEG VALUES( 6, 1, 'Melbourne', 'Sydney');
INSERT INTO TRKTRIPLEG VALUES( 6, 2, 'Sydney', 'Adelaide');
INSERT INTO TRKTRIPLEG VALUES( 6, 3, 'Adelaide', 'Sydney');
INSERT INTO TRKTRIPLEG VALUES( 7, 1, 'Melbourne', 'Sydney');
INSERT INTO TRKTRIPLEG VALUES( 7, 2, 'Sydney', 'Adelaide');
INSERT INTO TRKTRIPLEG VALUES( 7, 3, 'Adelaide', 'Sydney');
INSERT INTO TRKTRIPLEG VALUES( 8, 1, 'Sydney', 'Newcastle');
INSERT INTO TRKTRIPLEG VALUES( 8, 2, 'Newcastle', 'Brisbane');
INSERT INTO TRKTRIPLEG VALUES( 8, 3, 'Brisbane', 'Rockhampton');
INSERT INTO TRKTRIPLEG VALUES( 9, 1, 'Sydney', 'Melbourne');
INSERT INTO TRKTRIPLEG VALUES(10, 1, 'Sydney', 'Wollongong');
INSERT INTO TRKTRIPLEG VALUES(11, 1, 'Melbourne', 'Sydney');
INSERT INTO TRKTRIPLEG VALUES(12, 1, 'Melbourne', 'Sydney');
INSERT INTO TRKTRIPLEG VALUES(12, 2, 'Sydney', 'Melbourne');
INSERT INTO TRKTRIPLEG VALUES(13, 1, 'Sydney', 'Melbourne');
INSERT INTO TRKTRIPLEG VALUES(13, 2, 'Melbourne', 'Sydney');
INSERT INTO TRKTRIPLEG VALUES(14, 1, 'Wollongong', 'Sydney');
在 trktrip 上插入数据
INSERT INTO TRKTRIP VALUES( 1, 10001, 'PKR768', ADD_MONTHS(TRUNC(SYSDATE),0)-135 );
INSERT INTO TRKTRIP VALUES( 2, 10002, 'SYF777', ADD_MONTHS(TRUNC(SYSDATE),0)-135 );
INSERT INTO TRKTRIP VALUES( 3, 10001, 'KKK007', ADD_MONTHS(TRUNC(SYSDATE),0)-133 );
INSERT INTO TRKTRIP VALUES( 4, 10003, 'PKR768', ADD_MONTHS(TRUNC(SYSDATE),0)-130 );
INSERT INTO TRKTRIP VALUES( 5, 20002, 'PKR768', ADD_MONTHS(TRUNC(SYSDATE),0)-130 );
INSERT INTO TRKTRIP VALUES( 6, 30005, 'SYF777', ADD_MONTHS(TRUNC(SYSDATE),0)-124 );
INSERT INTO TRKTRIP VALUES( 7, 20005, 'KKK007', ADD_MONTHS(TRUNC(SYSDATE),0)-123 );
INSERT INTO TRKTRIP VALUES( 8, 10001, 'PKR768', ADD_MONTHS(TRUNC(SYSDATE),0)-123 );
INSERT INTO TRKTRIP VALUES( 9, 10002, 'QRT834', ADD_MONTHS(TRUNC(SYSDATE),0)-123 );
INSERT INTO TRKTRIP VALUES(10, 30005, 'KKK007', ADD_MONTHS(TRUNC(SYSDATE),0)-122 );
INSERT INTO TRKTRIP VALUES(11, 10003, 'SST005', ADD_MONTHS(TRUNC(SYSDATE),0)-122 );
INSERT INTO TRKTRIP VALUES(12, 10002, 'PKR768', ADD_MONTHS(TRUNC(SYSDATE),0)-122 );
INSERT INTO TRKTRIP VALUES(13, 20002, 'QRT834', ADD_MONTHS(TRUNC(SYSDATE),0)-120 );
INSERT INTO TRKTRIP VALUES(14, 20002, 'PKR008', ADD_MONTHS(TRUNC(SYSDATE),0)-119 );
*我的插入代码不完整,我只是将其中的一部分复制到 14 条记录,但足以解决这个问题。*
我的sql代码的问题是
如果我执行以下操作
select DISTINCT leg.destination from trktripleg leg where leg.T# IN (select T# from trktrip trip WHERE L#='10001' OR L#='10002');
没有leg.depature,它会显示正确的答案,但是如果我添加leg.depature,它将为答案创建另一列。
方法
destination ----- depature
但我想将这 2 个答案一起加入并成为 1,这将满足以下要求
查找驾照号码 (L#) 10001 和 10002 的两位司机至少访问过一次的所有城市(出发地或目的地)的名称。
我的输出:
SQL> select DISTINCT leg.destination,leg.departure from trktripleg leg where leg .T# IN (select T# from trktrip trip WHERE L#='10001' OR L#='10002');
DESTINATION DEPARTURE
------------------------------ ------------------------------
Sydney Adelaide
Sydney Melbourne
Perth Hobart
Adelaide Sydney
Perth Sydney
Perth Melbourne
Brisbane Newcastle
Newcastle Sydney
Adelaide Perth
Adelaide Melbourne
Brisbane Sydney
DESTINATION DEPARTURE
------------------------------ ------------------------------
Perth Adelaide
Wollongong Adelaide
Hobart Melbourne
Sydney Perth
Sydney Brisbane
Melbourne Perth
Rockhampton Brisbane
Melbourne Sydney
Sydney Wollongong
20 rows selected.
如果我只是按目的地找到,那将是
SQL> select DISTINCT leg.destination from trktripleg leg where leg.T# IN (select
T# from trktrip trip WHERE L#='10001' OR L#='10002');
DESTINATION
------------------------------
Sydney
Adelaide
Brisbane
Newcastle
Perth
Melbourne
Hobart
Rockhampton
Wollongong
9 rows selected.
但问题是我需要将目的地结果与出发合并。当我同时选择两者时,它也不再明显。
感谢所有帮助。