Hello, everyone:
How do you make an SQL statement that returns results modified by a subquery, or a join - or something else, that deals with information you're trying to return?
For example:
CREATE TABLE bowlers (
bowling_id int4 not null primary key auto_increment,
name text,
team text
);
Someone might incorrectly be on more than one team:
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');
So homer
cannot decide on his team, so he's on both. Do'h!
I want to know everyone who is on, the homer team
who is not also on the pin pals
team. The best I can do is this:
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');
Resulting in:
+-----------------------+----------------+
| 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)
Which, you know, brilliant!
The performance will suffer, as the subquery is going to be run for each result of the query, which is B to the A to the D. Great for a few rows, Pretty bad for the hundreds of thousands of rows.
What is a better way? I am mostly thinking a self join would do the trick, but I can't wrap my head around how to do that.
Are there any other ways to do this, without using, NOT IN( SELECT ... )
Also, what is the name for this type of problem?