无与伦比的记录

时间:2013-03-18 06:57:52

标签: sql join

我有两个表,我想显示MBA表中存在的但不在PIX表中保存的Id

PIX

 Id Channel Product ProgDate    ProgStartTime   ProgEndTime 
    2   PIX Parachute Advansed  2011-04-02  18:00:00.0000000    01:00:00.0000000
    5   PIX Parachute Advansed  2011-04-02  18:00:00.0000000    01:00:00.0000000
    6   PIX Parachute Advansed  2011-04-02  23:00:00.0000000    01:00:00.0000000

MBA

mbaID   Channel Product-MAP Progdate-MAP    AdvTime 
2   PIX Parachute Advansed  4/2/11  19:54:56
3   PIX Parachute Advansed  4/2/11  19:15:59
4   PIX Parachute Advansed  4/2/11  22:26:28
5   PIX Parachute Advansed  4/2/11  21:47:01
6   PIX Parachute Advansed  4/3/11  0:31:08
7   PIX Parachute Advansed  4/3/11  0:17:23

我需要显示3,4和7列。 但是,当我执行JOin时,它给了我重复。 请帮忙

2 个答案:

答案 0 :(得分:2)

带有LEFT JOIN谓词的表MBA

IS NULL仅显示另一个表PIX中没有条目的行。类似的东西:

SELECT MBA.*
FROM MBA 
LEFT JOIN PIX ON MBA.mbaID = pix.id
WHERE pix.Id IS NULL;

SQL Fiddle Demo

这将为您提供带有ID 3,4,7的行

| MBAID | CHANNEL | PRODUCTMAP | PROGDATEMAP |                      ADVTIME |
-----------------------------------------------------------------------------
|     3 |     PIX |  Parachute |    Advansed | April, 02 1911 21:15:59+0000 |
|     4 |     PIX |  Parachute |    Advansed | April, 03 1911 00:26:28+0000 |
|     7 |     PIX |  Parachute |    Advansed | April, 03 1911 02:17:23+0000 |

您还可以使用NOT IN谓词代替JOIN

SELECT MBA.*
FROM MBA 
WHERE mbaid NOT IN(SELECT id
                   FROM PIX 
                   WHERE id IS NOT NULL);

SQL Fiddle Demo

答案 1 :(得分:0)

使用SUBQUERY

select distinct mbaid from mba where mbaid not in (select distinct id from pix)

使用LEFT OUTER JOIN

select distinct mbaid from mba,pix where mba.mbaid=pix.id(+) and pix.id is null;