11

如何创建一个 SQL 语句来返回由子查询、连接或其他东西修改的结果,以处理您试图返回的信息?

例如:

CREATE TABLE bowlers (
bowling_id int4 not null primary key auto_increment,
name text,
team text
);

有人可能会错误地加入多个团队:

INSERT INTO `bowlers` (`name`, `team`) VALUES
('homer', 'pin pals'),
('moe', 'pin pals'),
('carl', 'pin pals'),
('lenny', 'pin pals'),
('homer', 'The homer team'),
('bart', 'The homer team'),
('maggie', 'The homer team'),
('lisa', 'The homer team'),
('marge', 'The homer team'),
('that weird french guy', 'The homer team');

所以homer不能决定他的团队,所以他在两者上。干!

我想知道每个在队里的人,the homer team谁不在pin pals队里。我能做的最好的是:

SELECT a.name, a.team 
    FROM bowlers a where a.team = 'The homer team' 
    AND a.name 
    NOT IN (SELECT b.name FROM bowlers b WHERE b.team = 'pin pals');

导致:

+-----------------------+----------------+
| name                  | team           |
+-----------------------+----------------+
| bart                  | The homer team | 
| maggie                | The homer team | 
| lisa                  | The homer team | 
| marge                 | The homer team | 
| that weird french guy | The homer team | 
+-----------------------+----------------+
5 rows in set (0.00 sec)

哪个,你知道的,太棒了!

性能将受到影响,因为将为查询的每个结果运行子查询,即 B 到 A 到 D。对于几行来说很好,对于数十万行来说非常糟糕。

有什么更好的方法?我主要认为自我加入可以解决问题,但我无法解决如何做到这一点。

有没有其他方法可以做到这一点,而不使用,NOT IN( SELECT ... )

另外,这类问题的名称是什么?

4

2 回答 2

15

像这样:

SELECT a.name, a.team
FROM bowlers a
LEFT OUTER JOIN bowlers b ON a.name = b.name AND b.team = 'pin pals'
WHERE a.team = 'The homer team'
AND b.name IS NULL;

你也可以这样做:

SELECT a.name, a.team
FROM bowlers a
WHERE a.team = 'The homer team'
AND NOT EXISTS (SELECT * FROM bowlers b
    WHERE b.team = 'pin pals'
    AND a.name = b.name
    );

顺便说一句,这被称为“左反半连接”。

于 2009-08-17T04:41:42.047 回答
2

您可以LEFT JOIN并确保连接的表中没有数据(一切都为空)。

SELECT a.name, a.team 
    FROM bowlers a
    LEFT JOIN bowlers b
        ON b.name = a.name AND b.team = 'pin pals'
    WHERE a.team = 'The homer team' 
    AND a.name
    -- the join has to fail for this to be null
    AND b.bowling_id IS NULL
于 2009-08-17T04:40:33.203 回答