首页 > 解决方案 > 子查询方程左边部分和右边部分一样,为什么?

问题描述

  \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 如果方程的左右部分相同,为什么放它。所以我没有这部分。

标签: postgresql

解决方案


两者results和都r指从 table 中选择results。然而,这是两个单独的选择,想象一下做这个查询。select r1.resultid as r1, r2.resultid as r2 from results r1, results r2. 如果表中有 n 行,则结果中会收到 n*n 行。


推荐阅读