Sql 为什么这两个选择COUNT(*)和否定WHERE子句的总和不正确?
我正在从带有where子句的表中选择行,但是否定where子句会返回更多的行 给定一个有1000行的表,aSql 为什么这两个选择COUNT(*)和否定WHERE子句的总和不正确?,sql,postgresql,Sql,Postgresql,我正在从带有where子句的表中选择行,但是否定where子句会返回更多的行 给定一个有1000行的表,a选择where返回600行,该的否定版本不应该选择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
选择where
返回600行,该的否定版本不应该选择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))
select count(*) from trips
where service_id in
(select service_id from calendar_dates
where date_part('dow', date) not in (5, 6))
返回363272
运行相同的查询,但用于周末未开始的旅行
select count(*) from trips
where service_id in
(select service_id from calendar_dates
where date_part('dow', date) in (5, 6))
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数据的数据库上完成的。我不知道我错过了什么。这不是否定的
如果您的服务id与dow=3、4、5、6
相同,则它将出现在2个计数中
正确的否定是
select count(*) from trips
where service_id NOT in
(select service_id from calendar_dates
where date_part('dow', date) in (5, 6))
或与不存在的等效项
不被否定
如果您的服务id与dow=3、4、5、6
相同,则它将出现在2个计数中
正确的否定是
select count(*) from trips
where service_id NOT in
(select service_id from calendar_dates
where date_part('dow', date) in (5, 6))
如果服务id
在与日期部分(5、6、7)相关联的日历日期中有3行,则它将由两个查询计数如果服务id
在与日期部分(5、6、7)相关联的日历日期中有3行,则它将由两个查询计数