Re: BUG #15783: Fail to select with a function in FROM clause plus another table

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: tosites2019(at)gmail(dot)com
Cc: pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #15783: Fail to select with a function in FROM clause plus another table
Date: 2019-04-28 04:55:12
Message-ID: 6702.1556427312@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

PG Bug reporting form <noreply(at)postgresql(dot)org> writes:
> select m.nome socio, p.tipo, sum(c.valor * m.percentual / 100) valor
> from contas_pagas('2019-04-01', '2019-04-30', 'R') c, medico m
> inner join planoconta p on (c.cod_planoconta = p.cod)
> where (m.percentual > 0)
> group by m.nome, p.tipo

> -- ERROR
> -- SQL Error [42P01]: ERROR: invalid reference to FROM-clause entry for
> table "c"
> -- Hint: There is an entry for table "c", but it cannot be referenced from
> this part of the query.

> In other words, we need to use cross join instead of put all tables in FROM
> clause, if one of these tables was a function that returns a table.

No, this has nothing to do with whether the relations are functions or
tables. You've forgotten that JOIN binds more tightly than comma in a
FROM-list, so that the relations available to that ON clause are only
m and p.

MySQL got this wrong for many years (maybe still does?), which has
encouraged a lot of confusion, but the SQL standard is entirely
clear on the point.

regards, tom lane

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message PG Bug reporting form 2019-04-28 06:47:22 BUG #15784: logical replication replicate data of type array for operation insert but update/delete/truncate
Previous Message PG Bug reporting form 2019-04-28 04:09:18 BUG #15783: Fail to select with a function in FROM clause plus another table