Skip to content
Advertisement

How to pass a variable to a subselect in a view

I have a table of posts, post_likes, and I need a query that will give me both totals for likes for posts, and also a given specific user’s likes for those posts. This means I need a good way of giving MY_USER_ID as input data.

Here’s a query that works

BUT my ORM (rust diesel) doesn’t allow me to set or query for that necessary MY_USER_ID field, since it’s a subquery.

I’d really love to be able to do something like:

Advertisement

Answer

Expose my_user_id on select clause of view

UPDATE

This can obtain post’s scores even when no user is given

When no user is passed, select the query denoted by my_user_id of ''

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement