简体   繁体   English

使用SELECT CASE和SUM:MYSQL

[英]Using SELECT CASE and SUM: MYSQL

I can't figure out how to SUM up and down votes on a certain item while also returning whether or not a given user voted on this item. 我无法弄清楚如何SUM上下票对某一个项目,同时返回给用户是否投票这个项目。

Here's my Votes table: 这是我的投票表:

item_id vote voter_id
1        -1     joe
1         1     bob
1         1     tom
3         1     bob

For item_id=1 here's the data I want to show: 对于item_id=1这是我要显示的数据:

If Joe is looking at the page: 如果Joe正在查看页面:

total up votes: 2
total down votes: 1
my_vote: -1

Bob : Bob

total up votes: 2
total down votes: 1
my_vote: 1

Here's my code: 这是我的代码:

SELECT MAX(CASE WHEN voter_id = 'joe' and vote=1 THEN 1 
WHEN voter_id = 'joe' and vote='-1' THEN -1
ELSE 0 END)
AS my_vote, item_id, sum(vote=1) yes, sum(vote='-1') no
FROM Votes
WHERE item_id=1

The issue is that my_vote=0 if I use the MAX function and vote=-1 (Joe's scenario). 问题是,如果我使用MAX函数, vote=-1 (Joe的场景),则my_vote=0 Similarly, my_vote=0 if I use the MIN function and the vote=1 (Bob's scenario). 同样,如果我使用MIN函数,而vote=1 (鲍勃的场景),则my_vote=0

Thoughts? 有什么想法吗?

SELECT
  item_id,
  MAX(CASE WHEN voter_id = 'Joe' THEN vote ELSE NULL END)   AS my_vote,
  sum(vote= 1)   AS yes_votes,
  sum(vote=-1)   AS no_votes
FROM
  Votes
WHERE
  item_id = 1
GROUP BY
  item_id


Or, possibly more flexible... 或者,可能更灵活...

SELECT
  Votes.item_id,
  MAX(CASE WHEN Users.user_id IS NOT NULL THEN Votes.vote ELSE NULL END)   AS my_vote,
  sum(Votes.vote= 1)   AS yes_votes,
  sum(Votes.vote=-1)   AS no_votes
FROM
  Votes
LEFT JOIN
  Users
    ON  Users.user_id = Votes.voter_id
    AND Users.user_id = 'Joe'
WHERE
  Votes.item_id = 1
GROUP BY
  Votes.item_id

You can use correlated subquery to get the user vote and assuming that the user can only vote once per item_id . 您可以使用相关子查询来获得用户投票,并假设用户每个item_id只能投票一次。

SELECT  SUM(vote = 1) totalUpVotes,
        SUM(vote = -1) totalDownVotes,
        (SELECT MAX(vote) 
        FROM votes b
        WHERE a.item_id = b.item_id AND
                b.voter_id = 'joe') my_vote
FROM    votes a
WHERE   item_id = 1

My answer's pretty much the same, but for those of us who love stored procs I've cleaned it up a little: 我的回答几乎是相同的,但是对于那些喜欢存储过程的人来说,我已经对其进行了一些清理:

declare p_item_id int;
declare p_voter_id varchar(10);

select 
    sum(vote = 1) as TotalUpVotes
    , sum(vote = -1) as TotalDownVotes
    , sum(vote) as TotalScore
    , sum(case when p_voter_id = voter_id then vote else 0 end) as my_vote
from Votes v
where p_item_id = v.item_id

You can use something like this 你可以用这样的东西

select 'total up votes' as descr, count(*) as num from Votes where item_id=1 and vote=1 union all select 'total down votes', count(*) from Votes where item_id=1 and vote=-1 union all select 'my_vote' as descr, vote from Votes where item_id=1 and voter_id = 'joe' 选择“总投票数”作为descr,从votes的count(*)作为num的项(其中item_id = 1和vote = 1联合)全部选择“总投票数”,从votes的计数(*),其中item_id = 1和vote = -1联合所有人都选择“ my_vote”作为描述,从“ item_id = 1”和“ voter_id =” joe”的投票中投票

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

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