0
我使用PostgreSQL WITH
https://www.postgresql.org/docs/current/static/queries-with.html。我可以运行相当复杂的查询,但是我可以使用已排序的数据吗?例如:PostgreSQL WITH查询已经选定的表
SELECT id, f_name, l_name, email FROM users
WHERE f_name = 'BabyBoy'
WITH i_need_do_thomething AS (
# and than use that filtered data
)
SELECT * FROM i_need_do_thomething
感谢
UPDATE
,我在Ruby中使用此查询on Rails的所有问题。我用联想,如:
@laptop = Laptop.find(1)
@laptop.user.do_custom_sql(HERE_WILL_BE_RAW_SQL)
其中@laptop.user
等于
SELECT id, f_name, l_name, email FROM users
WHERE f_name = 'BabyBoy'
因此,我只能在单独的情况下使用'WITH'?我不能像我在示例中所写的那样使用它。对? –
是的,你不能这样做。在这种情况下,我认为你应该使用'ActiveRecord :: Base.connection.execute(whole_sql_here)',然后遍历结果集。 –