简体   繁体   English

如何将变量传递给视图中的子选择

[英]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. 我的帖子,post_likes的表,我需要一个查询,这将使我都为汇总喜欢的岗位, 也是一个给定的特定用户的喜好对这些职位。 This means I need a good way of giving MY_USER_ID as input data. 这意味着我需要一种很好的方式来提供MY_USER_ID作为输入数据。

Here's a query that works 这是一个有效的查询

create view post_view as 
select post.id,
       coalesce(sum(post_like.score),0) as score,
       (
         select score 
         from post_like 
         where post.id = post_like.post_id 
         and post_like.fedi_user_id = MY_USER_ID 
       ) as my_vote,
from post
  left join post_like on post.id = post_like.post_id
group by post.id;

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. 但是我的ORM(防锈柴油)不允许我设置或查询该必需的MY_USER_ID字段,因为它是子查询。

I'd really love to be able to do something like: 我真的很想能够做类似的事情:

select * 
from post_view 
where my_user_id = 'X';

you can move that condition in on clause 您可以在on子句中移动该条件

create view post_view as 
select post.id,
coalesce(sum(post_like.score),0) as score
from post
left join post_like
on post.id = post_like.post_id and  post_like.fedi_user_id = MY_USER_ID
group by post.id;

Expose my_user_id on select clause of view 在视图的选择子句中公开my_user_id

-- get all of the user's score on all post, regardless of the user liking the post or not

create view post_view as

select 
    u.id as my_user_id,         
    p.id as post_id, 
    sum(pl.score) over (partition by p.id) as score,
    coalesce(pl.score, 0) as my_vote -- each u.id's vote
from user u
cross join post p
left join post_like pl on u.id = pl.fedi_user_id and p.id = pl.post_id;


select * from post_view where my_user_id = 'X';

UPDATE UPDATE

This can obtain post's scores even when no user is given 即使没有用户,也可以获得帖子的分数

create view post_view as

with all_post as
(
    select        
        p.id as post_id,
        sum(pl.score) as score
    from post p
    left join post_like pl on p.id = pl.post_id
    group by p.id
)

select
    u.id as my_user_id,
    ap.post_id,
    ap.score,
    coalesce(pl.score, 0) as my_vote
from user u
cross join all_post ap
left join post_like pl on u.id = pl.fedi_user_id and ap.post_id = pl.post_id

union all

select 
    '' as my_user_id, 
    ap.post_id, 
    ap.score, 
    0 as my_vote
from all_post ap
;


select * from post_view where my_user_id = 'X';

When no user is passed, select the query denoted by my_user_id of '' 如果未传递任何用户,请选择''的my_user_id表示的查询

select * from post_view where my_user_id = '';

You can move the logic to the select using conditional aggregation: 您可以使用条件聚合将逻辑移至select

select p.id,
       coalesce(sum(pl.score), 0) as score,
       sum( (pl.fedi_user_id = MY_USER_ID)::int ) as my_vote
from post p left join
     post_like pl
     on p.id = pl.post_id
group by p.id;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM