3表,JOIN查询和字母顺序

时间:2015-03-02 05:40:04

标签: sql

我目前正在使用三个表格,我试图弄清楚如何使用联接来显示任何一本书的title_id与Dennis McCann作为编辑。这些表格共有title_ideditor_id。无法找到一种方法将它们拼凑在一起。如何以Dennis McCann为编辑的任何一本书的title_id显示?

  

SELECT * FROM title_editors;

 EDITOR_ID   TITLE_ EDITOR_ORDER
    ----------- ------ ------------
    826-11-9034 Bu2075            2
    826-11-9034 PS2091            2
    826-11-9034 Ps2106            2
    826-11-9034 PS3333            2
    826-11-9034 PS7777            2
    826-11-9034 pS1372            2
    885-23-9140 MC2222            2
    885-23-9140 MC3021            2
    885-23-9140 Tc3281            2
    885-23-9140 TC4203            2
    885-23-9140 TC7777            2
    321-55-8906 bU1032            2
    321-55-8906 BU1111            2
    321-55-8906 BU7832            2
    321-55-8906 PC1035            2
    321-55-8906 PC8888            2
    321-55-8906 BU2075            3
    777-02-9831 pc1035            3
    777-02-9831 PC8888            3
    943-88-7920 BU1032            1
    943-88-7920 bu1111            1
    943-88-7920 BU2075            1
    943-88-7920 BU7832            1
    943-88-7920 PC1035            1
    943-88-7920 pc8888            1
    993-86-0420 PS1372            1
    993-86-0420 PS2091            1
    993-86-0420 PS2106            1
    993-86-0420 PS3333            1
    993-86-0420 pS7777            1
    993-86-0420 MC2222            1
    993-86-0420 MC3021            1
    993-86-0420 Tc3218            1
    993-86-0420 TC4203            1
    993-86-0420 TC7777            1

35 rows selected.
  

SQL> SELECT * FROM title_authors;

AUTHOR_ID   TITLE_ AUTHOR_ORDER ROYALTY_SHARE
----------- ------ ------------ -------------
409-56-7008 Bu1032            1            .6
486-29-1786 PS7777            1             1
486-29-1786 pC9999            1             1
712-45-1867 MC2222            1             1
172-32-1176 Ps3333            1             1
213-46-8915 BU1032            2            .4
238-95-7766 PC1035            1             1
213-46-8915 Bu2075            1             1
998-72-3567 pS2091            1            .5
899-46-2035 PS2091            2            .5
998-72-3567 PS2106            1             1
722-51-5454 mc3021            1           .75
899-46-2035 MC3021            2           .25
807-91-6654 tC3218            1             1
274-80-9391 BU7832            1             1
427-17-2319 pC8888            1            .5
846-92-7186 PC8888            2            .5
756-30-7391 PS1372            1           .75
724-80-9391 PS1372            2           .25
724-80-9391 bu1111            1            .6
267-41-2394 bU1111            2            .4
672-71-3249 TC7777            1            .4
267-41-2394 TC7777            2            .3
472-27-2349 Tc7777            3            .3
648-92-1872 TC4203            1             1

25 rows selected.
  

SQL> SELECT * FROM编辑;

EDITOR_ID   EDITOR_LNAME      EDITOR_FNAME  EDITOR_POSITION PHONE        ADDRESS              CITY      ST ZIP
----------- ----------------- ------------- --------------- ------------ -------------------- ------------ -- ------
321-55-8906 DeLongue          Martinella    Project         415 843-2222 3000 6th St.         BERKELEY     Ca 94710
723-48-9010 Sparks            MANfred       cOPY            303 721-3388 15 Sail              DENVER    Co 80237
777-02-9831 Samuelson         Bernard       proJect         415 843-6990 27 Yosemite          OAKLAND      Ca 94609
777-66-9902 Almond            Alfred        copy            312 699-4177 1010 E. DeVON        CHICAGO      Il 60018
826-11-9034 Himmel            Eleanore      pRoject         617 423-0552 97 Bleaker           BOSTON    Ma 02210
885-23-9140 Rutherford-Hayes  Hannah        PROJECT         301 468-3909 32 Rockbill Pike     ROCKBILL     MD 20852
993-86-0420 McCann            Dennis        acQuisition     301 468-3909 32 Rockbill Pike     ROCKBill     MD 20852
943-88-7920 Kaspchek          Christof      acquisitiOn     415 549-3909 18 Severe Rd.        BERKELEY     CA 94710
234-88-9720 Hunter            Amanda        acquisition     617 432-5586 18 Dowdy Ln.         BOSTON    MA 02210

2 个答案:

答案 0 :(得分:2)

您可以使用join在表格EditorsTtile_Editors上尝试Editor_ID,这会为您提供匹配的记录,您可以过滤掉仅用于&#39 ;丹尼斯麦肯'在连接中使用多个条件或在where子句中使用

没有

 SELECT DISTINCT te.title_id,ed.EDITOR_ID,ed.EDITOR_LNAME,ed.EDITOR_FNAME
 FROM
    title_editors te JOIN editors ed
     ON te.EDITOR_ID = ed.EDITOR_ID 
        AND ed.EDITOR_LNAME = 'McCann'
        AND ed.EDITOR_FNAME = 'Dennis'
 ORDER BY te.title_id

使用WHERE

  SELECT DISTINCT te.title_id,ed.EDITOR_ID,ed.EDITOR_LNAME,ed.EDITOR_FNAME
 FROM
    title_editors te JOIN editors ed
     ON te.EDITOR_ID = ed.EDITOR_ID 
 WHERE 
      ed.EDITOR_LNAME = 'McCann'
      AND ed.EDITOR_FNAME = 'Dennis'
 ORDER BY te.title_id

答案 1 :(得分:0)

使用in运算符会更容易:

SELECT   DISTINCT title_id
FROM     title_editors
WHERE    editor_id IN (SELECT editor_id
                       FROM   editors
                       WHERE  editor_fname = 'Dennis' AND
                              editor_lname = 'McCann')
ORDER BY title_id ASC