MYSQL来自2个字段和不同值的select语句

时间:2012-11-25 09:37:08

标签: mysql

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 );

* 我的INSERT代码不完整,我只是将其中的一部分复制到14条记录,但足以满足这个问题。 *

我的sql代码的问题是

如果我执行以下操作

从trktripleg leg中选择DISTINCT leg.destination leg.T#IN(从trktrip trip选择T#,其中L#='10001'或L#='10002');

没有leg.depature,它显示正确答案,但如果我添加leg.depature,它将为答案创建另一列。

表示

destination ----- depature

但我想加入这2个答案并成为1,这将实现以下

查找具有许可证号(L#)10001和10002的两位司机至少访问过的所有城市(出发地或目的地)的名称。

我的输出:

SQL>从腿部的trktripleg腿中选择DISTINCT leg.destination,leg.departure .T#IN(从trktrip旅行中选择T#,其中L#='10001'或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.

但问题是我需要合并目的地结果与出发。当我选择两者时,它也不再明显。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

select DISTINCT leg.destination city from trktripleg leg where leg.T# IN (select T# from trktrip trip WHERE L#='10001' OR L#='10002')
UNION
select DISTINCT leg.departure city from trktripleg leg where leg.T# IN (select T# from trktrip trip WHERE L#='10001' OR L#='10002')