题
考虑如下表:
mysql> select * from phone_numbers;
+-------------+------+-----------+
| number | type | person_id |
+-------------+------+-----------+
| 17182225465 | home | 1 |
| 19172225465 | cell | 1 |
| 12129876543 | home | 2 |
| 13049876543 | cell | 2 |
| 15064223454 | home | 3 |
| 15064223454 | cell | 3 |
| 18724356798 | home | 4 |
| 19174335465 | cell | 5 |
+-------------+------+-----------+
我试图找到那些谁拥有家庭电话,但没有人的细胞。
这个查询的工作:
mysql> select h.*
-> from phone_numbers h
-> left join phone_numbers c
-> on h.person_id = c.person_id
-> and c.type = 'cell'
-> where h.type = 'home'
-> and c.number is null;
+-------------+------+-----------+
| number | type | person_id |
+-------------+------+-----------+
| 18724356798 | home | 4 |
+-------------+------+-----------+
但是这一个不会:
mysql> select h.*
-> from phone_numbers h
-> left join phone_numbers c
-> on h.person_id = c.person_id
-> and h.type = 'home'
-> and c.type = 'cell'
-> where c.number is null;
+-------------+------+-----------+
| number | type | person_id |
+-------------+------+-----------+
| 19172225465 | cell | 1 |
| 13049876543 | cell | 2 |
| 15064223454 | cell | 3 |
| 18724356798 | home | 4 |
| 19174335465 | cell | 5 |
+-------------+------+-----------+
两者之间唯一的区别是的h.type = 'home'
条件的位置 - 在该第一它的where
子句中和在所述第二它的on
子句的一部分
为什么没有所述第二查询返回相同的结果作为第一?
解决方案
在第二SQL,条件h.type =“家”是外部的一部分的连接条件,并且不是对结果的过滤器。对于所有的记录,其中h.type =“细胞”,条件h.type =“家”是假,因此不“匹配” C行发现 - 所以c.number为空,这是你唯一的筛选(WHERE)条件
在伪代码的第二次SQL是这样的:
for each row in phone_numbers h /* Note this is ALL home AND cell phones */
select c.number from phone_numbers c
where h.person_id = c.person_id
and h.type = 'home'
and c.type = 'cell';
if c.number is null (i.e. no row found)
display h.*
end if
end loop;
其他提示
在做左加入我做事这种方式。在加入您需要指定链接实际上从右侧两个表一起,任何过滤条件(第2表中加入)的ANNY领域的连接(有一个例外,我会在短期内)。从加入(第1表)的左侧筛选条件应在where子句中否则将错误地影响加入,你看到(和托尼这么好解释)。唯一的时间的右侧加入应在where子句是如果要在该表中寻找空值(即,它们是在第一个表而不是第二记录)。
SEL *
FROM phone_numbers T1
WHERE typeS='home' AND person_id NOT IN
(SELECT person_id FROM phone_numbers T2 WHERE T1.person_id=T2.person_id AND typeS='cell')
您可以尝试此查询,我希望它会为你工作。
select * from phone_numbers
where person_id not in (select person_id from phone_numbers where type='cell')
我不知道这是否会解决的事情或没有,但...
开始的陈述“和”应该是WHERE子句,而不是ON子句的一部分的一部分。 ON子句应的仅强>具有涉及该列用于联接表的语句。
不隶属于 StackOverflow