Discussion:
Cartesian product
remcob
2008-02-17 13:16:39 UTC
Permalink
Is there an easy way to create a cartesian product?

Like:
SELECT t1.name, t2.name FROM names t1, names t2

I don't use cartesian products all the time, but sometimes I'd like to
use them (for example to use Joe Celkoo's nested set queries)
Sharon Rosner
2008-02-17 16:09:26 UTC
Permalink
Post by remcob
Is there an easy way to create a cartesian product?
SELECT t1.name, t2.name FROM names t1, names t2
sure:

DB.select(:t1__name.as(:n1), :t2__name.as(:n2)).from('names t1,
names t2')

But in that case you'd be typing less by executing raw sql:

DB['SELECT t1.name AS n1, t2.name AS n2 FROM names t1, names t2']

Just note that you have to alias the columns.

sharon

Loading...