I'm just getting started with Postgres. Reading this document I came across this query:
SELECT title, ts_rank_cd(textsearch, query) AS rank
FROM apod, to_tsquery('neutrino|(dark & matter)') query
WHERE query @@ textsearch
ORDER BY rank DESC
LIMIT 10;
I can understand everything in this query, except for this: FROM apod, ...
.
What does this ,
mean? I'm used to joins but not to multiple FROM
statements separated by a comma.
I searched the net for no avail. After looking at it and thinking, it seems to me that it's declaring a variable called query so it can use it multiple times. But if this is true, what does it have to do with FROM
?
Best Answer
It creates an implicit
CROSS JOIN
. It's the SQL-89 syntax.Here I use
values(1)
andvalues(2)
to create pseduo-tables (value tables) merely for examples. The thing after themt(x)
, andg(y)
are called FROM-Aliases the character inside the parenthesis is the alias for the column (x
andy
respectively). You could just as easily create a table to test this.Here is how you'd write it now.
From there you can make this an implicit
INNER JOIN
by adding a conditional.Or the explicit and newer
INNER JOIN
syntax,So in your example..
This is essentially the same as the newer syntax,
which is actually the same, in this case, because
to_tsquery()
returns a row and not a set as,However, the above could potentially cause
to_tsquery('neutrino|(dark & matter)')
to occur twice, but in this case it doesn't --to_tsquery
is marked as STABLE (verified with\dfS+ to_tsquery
).For a more complete comparison of the differences between SQL-89, and SQL-92, see also my answer here