我使用where子句从表中选择行,但是否定where子句会返回更多的行。
假设一个包含1000行的表,select where
返回600行,那么该select where
的反版本不应该返回400行吗?
select count(*) from trips
trips表包含420444
行。
我选择了周末出发的所有行程。
select count(*) from trips
where service_id in
(select service_id from calendar_dates
where date_part('dow', date) in (5, 6))
返回363272
运行相同的查询,但不是在周末开始的旅行
select count(*) from trips
where service_id in
(select service_id from calendar_dates
where date_part('dow', date) not in (5, 6))
返回377326
363272
+ 377326
= 740598
,这比420444
多得多
当涉及where子句中的子查询时,count
函数的行为是否有所不同?
这是在具有GTFS data https://developers.google.com/transit/gtfs/的数据库上完成的。我不知道我错过了什么。
发布于 2019-01-22 11:18:44
它没有被否定。
如果您有与dow = 3, 4, 5, 6
相同的service_id,那么它将出现在两个计数中。
正确的否定是
select count(*) from trips
where service_id NOT in
(select service_id from calendar_dates
where date_part('dow', date) in (5, 6))
或与not exists
等效的
https://stackoverflow.com/questions/54307072
复制相似问题