postgresql - 子查询方程左边部分和右边部分一样,为什么?
问题描述
\set season 'date ''1978-01-01'''
select forename,surname, constructors.name as constructor,
count(*) as races,
count(distinct status) as reasons
from drivers join results using(driverid)
join races using(raceid)
join status using(statusid)
join constructors using(constructorid)
where date >= :season and
date < :season + interval '1 year'
and not exists
(select 1from results r where position is not null and r.driverid = drivers.driverid
and r.resultid = results.resultid) group by constructors.name, driverid
order by count(*) desc;
内联子查询:
(select 1 from results r where position is not null and r.driverid = drivers.driverid and r.resultid = results.resultid)
,R参考表:结果,所以最后一部分变成results.resultid = results.resultid
如果方程的左右部分相同,为什么放它。所以我没有这部分。
解决方案
两者results
和都r
指从 table 中选择results
。然而,这是两个单独的选择,想象一下做这个查询。select r1.resultid as r1, r2.resultid as r2 from results r1, results r2
. 如果表中有 n 行,则结果中会收到 n*n 行。
推荐阅读
- python - 在 Python 中读取 NFC
- angular - 如何在文档中下载角度材料?
- php - 为什么当我从数据库中删除图像时也没有从服务器文件夹中删除?
- linux - 使用 '&' 运行的带有 2 个函数的循环仍然连续运行它们
- javascript - 使用 RxJS 的 switchMap 限制请求
- javascript - 如何在 Android 中查找导致 Cordova 应用程序崩溃的原因?
- python - 不使用 datetime 模块,即使这样,TypeError: 'DatetimeIndex'
- asp.net-mvc - 从 MVC 控制器启动网页
- java - spring data RepositoryRestResource 未映射到spring boot应用程序中的端点
- android - 创建新子项时,Recyclerview 会不断从 Firebase DB 重新填充数据